2015-04-24 21 views

回答

1

是的,當然是有可能的。例如:

# 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將用作排序鍵。

+0

什麼?只有這個??? *。*正在嘗試... –

+0

已用完將打開列表?字典?陣列?我需要爲(文本行)做 :? –

+0

'read()'將整個文件內容讀入到字符串「text」中。我將它分成標籤('\ t')中的一個數組,並將第三個元素(索引2)分配給變量'spent'。''spent'是一個字符串。這是非常基本的東西,雖然... – Felk

0
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!

相關問題