如何按列以数字方式对文本文件进行排序?

时间:2017-11-08 04:22:44

标签: python-3.x sorting text

from lxml import html
import operator
import discord
import yaml
import csv



raw_json = 
requests.get('https://bittrex.com/api/v1.1/public/getmarketsummaries').text
json_dict = json.loads(raw_json)
stuff = json_dict["result"]
new = []
for i in range(0,197):
    price = (stuff[i]['Last'])
    name1 = (stuff[i]['MarketName'])
    name = name1.replace("BTC-", "")
    prev = (stuff[i]['PrevDay'])
    diff = price - prev
    change = round(((price - prev) / price) * 100, 2)
    final = ('{0},{1}'.format(name,change))
    new.append(final)
butFirst = new[0:]
this1 = ("\n".join(butFirst))
text_file = open("Sort.txt", "w")
text_file.write(this1)
text_file.close()

我在第二列中排序此输出时出现问题。 我得到基数10错误..整数错误等..我认为问题 数字是如何存储但我无法弄明白。
输出看起来像这样>

1ST,-5.94
2GIVE,3.45
ABY,2.44
ADA,0.0
ADT,-4.87
ADX,-13.09
AEON,-2.86
AGRS,-2.0

1 个答案:

答案 0 :(得分:0)

您应该避免在需要之前将数据更改为文本。如果您使用词典列表进行操作,则可以非常轻松地对列表进行排序。

$ ./bin/menureadint

 r) Read data from file
 a) Print out the values
 b) Find the maximum Value
 c) Calculate the RMS (root mean square)
 d) Count the number of negative values
 e) Exit Back to Main Menu

Enter selection: a
error: data is empty.

 ... <menu - snip>

Enter selection: z
error: invalid input - choice.

 ... <menu - snip>

Enter selection: r

enter filename: dat/intvalues.txt

 ... <menu - snip>

Enter selection: a
data[  0]: 1
data[  1]: 6
data[  2]: 4
data[  3]: 5
data[  4]: 5
data[  5]: 9
data[  6]: 12
data[  7]: 14
data[  8]: 15
data[  9]: -17
data[ 10]: -19
data[ 11]: 21
data[ 12]: -23
data[ 13]: 0
data[ 14]: 37
data[ 15]: 0
data[ 16]: -31
data[ 17]: 32
data[ 18]: 34
data[ 19]: -37
data[ 20]: -39
data[ 21]: 0

 ... <menu - snip>

Enter selection: b
max value: 37

 ... <menu - snip>

Enter selection: c
RMS not yet implemented.

 ... <menu - snip>

Enter selection: e

successfully processed '22' integers from file.