2016-03-23 79 views
3

我試圖在將結果寫入新文件之前創建一個合併2個文本文件並對它們進行排序的函數。我已經閱讀了關於排序文件的現有線索以及關於合併文件的線索,但是我一直無法找到能夠解答我的問題的線索。在Python中合併兩個文件並對其進行排序

File1: 
12:24:00: 14, 15, 16 
20:13:09: 1, 2, 3 

File2: 
08:06:02: 43, 54, 10 
15:16:05: 6, 2, 12 

和所需的輸出會是這樣:

NewFile: 
20:13:09: 1, 2, 3 
15:16:05: 6, 2, 12 
12:24:00: 14, 15, 16 
08:06:02: 43, 54, 10 

我原本想這兩個文件的內容合併到一個列表,然後對它進行排序,將其寫入到一個新的文件之前,但這似乎沒有工作。這是我迄今爲止所嘗試的:

def mergeandsort(file1, file2, NewFile): 
    s1, s2, d=open(src1, 'r'), open(src2, 'r'), open(dst, 'w') 
    l=[] 
    l.append(list(s1)) 
    l.append(list(s2)) 
    n=sorted(l) 
    c=''.join(str(n)) 
    d.write(c) 
    s1.close(); s2.close(); d.close() 

我是新來的Python,所以任何幫助,將不勝感激!

+0

排序是根據時間?每行包含時間並且後跟3個整數值? –

+0

是的,我必須編寫一個函數,它只是對一個文件進行排序,並使用「排序」函數根據前三個值(即構成時間)進行排序。 – sophia

+0

好的,我會在接下來的10到15個薄荷糖中給你。 –

回答

4

試圖修復您的實現:

def mergeandsort(src1, src2, dst): 
    # Use `with` statements to close file automatically 
    with open(src1, 'r') as s1, open(src2, 'r') as s2, open(dst, 'w') as d: 
     l = list(s1) + list(s2) 
     l.sort(reverse=true) # Since you seem to want them in reverse order... 
     c = ''.join(l) 
     d.write(c) 

請注意,如果您處理大文件,這是不是最佳...

4

下面是步驟:

  1. 讀文件,然後創建有數據的列表。
  2. 添加兩個列表
  3. 如此排序的排序功能。
  4. 使用列表的反向方法
  5. 將內容寫入文件。 (你可以做到這一點)

演示

>>> p1 = '/home/vivek/Desktop/f1.txt' 
>>> p2 = '/home/vivek/Desktop/f2.txt' 
>>> 
>>> fp1 = open(p1) 
>>> fp2 = open(p2) 

>>> l1 = fp1.read().strip().split("\n") 
>>> l1 
['12:24:00: 14, 15, 16', '20:13:09: 1, 2, 3'] 
>>> l2 = fp2.read().strip().split("\n") 
>>> l2 
['08:06:02: 43, 54, 10', '15:16:05: 6, 2, 12'] 
>>> l3 = l1+ l2 
>>> l3 
['12:24:00: 14, 15, 16', '20:13:09: 1, 2, 3', '08:06:02: 43, 54, 10', '15:16:05: 6, 2, 12'] 
>>> sorted(l3) 
['08:06:02: 43, 54, 10', '12:24:00: 14, 15, 16', '15:16:05: 6, 2, 12', '20:13:09: 1, 2, 3'] 
>>> merge_list = sorted(l3) 
>>> merge_list.reverse() 
>>> merge_list 
['20:13:09: 1, 2, 3', '15:16:05: 6, 2, 12', '12:24:00: 14, 15, 16', '08:06:02: 43, 54, 10'] 
>>> 

功能

def mergeandsort(file1, file2, output): 
    fp1, fp2 = open(file1, 'r'), open(file2, 'r') 
    merge_data = fp1.read().strip().split("\n") + fp2.read().strip().split("\n") 
    merge_data = sorted(l3, reverse=True) 
    fp = open(output, 'w') 
    for i in merge_data: 
     fp.write(i) 

    fp.close() 
    return True, output 


p1 = '/home/vivek/Desktop/f1.txt' 
p2 = '/home/vivek/Desktop/f2.txt' 
p3 = '/home/vivek/Desktop/f12.txt' 

print mergeandsort(p1, p2, p3) 
+0

謝謝!當我把它寫入另一個文件時,我該怎麼做才能讓結果回到被格式化成行的狀態? – sophia

+1

當寫 – Bahrom

+0

ok時,使用''\ n'.join(merge_list)'。這樣做。不需要thanx :) –

2

下面是各種基於datetime替代(假設你讀的F1的內容和f2爲兩個列表,L1和L2):

l1 = ['12:24:00: 14, 15, 16', '20:13:09: 1, 2, 3'] 
l2 = ['08:06:02: 43, 54, 10', '15:16:05: 6, 2, 12'] 

from datetime import datetime 

for x in sorted(l1 + l2, key=lambda time_and_nums: datetime.strptime(time_and_nums.split(' ')[0][:-1], '%H:%M:%S'), reverse=True): 
    print(x) 

將打印

20:13:09: 1, 2, 3 
15:16:05: 6, 2, 12 
12:24:00: 14, 15, 16 
08:06:02: 43, 54, 10 
+0

是的,我們需要按時間值進行排序。 upvoted。好 –

相關問題