2015-12-31 40 views
0

我在文本文件中有以下數據。並行排序文本文件中的數據

[Test id_g001,**Test id_g002,Test id_g000, Value_is_0, Value_is_2, Value_is_1] 

我只能對數據進行排序。如何對數據進行並行排序,因此數據將按以下方式排序?無論是測試ID價值,需要進行排序

Test ID ---------------Value   
g000 ------------------- 0 
g001 ------------------- 1 
g002 ------------------- 2 

的代碼是:

def readFile(): 
from queue import PriorityQueue 
q = PriorityQueue() 
#try block will execute if the text file is found 
try: 
    fileName= open("textFile.txt",'r') 
    for line in fileName: 
      for string in line.strip().split(','): 
       q.put(string[-4:]) 
    fileName.close() #close the file after reading   
    print("Displaying Sorted Data") 
    while not q.empty(): 
     print(q.get()) 
     #catch block will execute if no text file is found 
except IOError: 
      print("Error: FileNotFoundException") 
      return 
+1

你需要閱讀文件中的元組列表,然後對第二個項目進行排序,該問題已經在這裏得到解答,所以我不會回答http://stackoverflow.com/questions/3121979/how- to-sort-list-tuple-of-lists-tuples – PyNEwbie

+0

捕獲IOError和print(「Error:FileNotFoundException」)是非常具有誤導性的。 – GingerPlusPlus

+0

使用csv lib來讀取您的csv文件 –

回答

0

比方說,你有作爲初級講座字符串列表:

>>> l = ['Test id_g001','**Test id_g002','Test id_g000', 'Value_is_0', 'Value_is_2', 'Value_is_1'] 

而你要根據ID和Value的值對它們進行分類,那麼你可以這樣做:

>>> from operator import itemgetter 
>>> sorted(l, key=itemgetter(-1)) 
['Test id_g000', 'Value_is_0', 'Test id_g001', 'Value_is_1', '**Test id_g002', 'Value_is_2'] 
相關問題