[英]how to join multiple sorted files in Python alphabetically?
如何逐行讀取多個CSV輸入文件,比較每行中的字符,將第一行按字母順序寫入輸出文件,然后前進最小值文件的指針,繼續與所有文件進行比較,直到結束到達所有輸入文件。 這是一個針對解決方案的粗略計划。
buffer = []
for inFile in inFiles:
f = open(inFile, "r")
line = f.next()
buffer.append([line, inFile])
#find minimum value in buffer alphabetically...
#write it to an output file...
#how do I advance one line in the file with the min value?
#and then continue the line-by-line comparisons in input files?
你可以使用heapq.merge
:
import heapq
import contextlib
files = [open(fn) for fn in inFiles]
with contextlib.nested(*files):
with open('output', 'w') as f:
f.writelines(heapq.merge(*files))
在Python 3.x(3.3+)中:
import heapq
import contextlib
with contextlib.ExitStack() as stack:
files = [stack.enter_context(open(fn)) for fn in inFiles]
with open('output', 'w') as f:
f.writelines(heapq.merge(*files))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.