[data.txt中]如何對特定數據/列上的整個文件進行排序?
CODE\tUSERNAME\tSPENT\tCOLUM1\tCOLUM2
我想文件[data.txt中]排序,使用 「失效」。我怎樣才能做到這一點?
[data.txt中]如何對特定數據/列上的整個文件進行排序?
CODE\tUSERNAME\tSPENT\tCOLUM1\tCOLUM2
我想文件[data.txt中]排序,使用 「失效」。我怎樣才能做到這一點?
是的,當然是有可能的。例如:
# read file into array of lines
lines = open("data.txt").readlines()
# sort those lines using a lambda
lines.sort(key = lambda line : line.split("\t")[2])
拉姆達擠出從該行的列SPENT
將用作排序鍵。
什麼?只有這個??? *。*正在嘗試... –
已用完將打開列表?字典?陣列?我需要爲(文本行)做 :? –
'read()'將整個文件內容讀入到字符串「text」中。我將它分成標籤('\ t')中的一個數組,並將第三個元素(索引2)分配給變量'spent'。''spent'是一個字符串。這是非常基本的東西,雖然... – Felk
def subMenu_5():
# read file into array of lines
lines = open("database").readlines()
# sort those lines using a lambda
lines.sort(key = lambda line : line.split("\t")[3])
clientList = []
dataQuantify = 0
database = open('database','r')
i = 1
while (i == 1):
if (database.readline() == ''):
i = 0
else:
dataQuantify = dataQuantify + 1
database.close()
sortList = open("sortList","w")
for i in range (3):
sortList.write(lines[i])
sortList.close()
print "[Código] [Nome] [Quant. Prod. Comprados] [Valor Gasto] [Descontos] \n"
sortList = open('sortList','r')
i = 0
while (i < dataQuantify):
clientList.append(sortList.readline())
print clientList[i]
i = i + 1
database.close()
raw_input("Precione 'ENTER' para voltar ao menu principal... ")
return
這項工作!非常Thx!
你可以使用'split()'或regex're' –