并行排序文本文件中的数据

时间:2015-12-31 23:05:26

标签: python sorting io

我在文本文件中有以下数据。

[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 个答案:

答案 0 :(得分: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']