2012-07-02 10 views
32

मेरे पास दो अलग-अलग भाषाओं में 2 टेक्स्टफाइल हैं और वे रेखा से लाइन गठबंधन हैं। अर्थात। textfile1 में पहली पंक्ति textfile2 में पहली पंक्ति के बराबर होनी चाहिए, और इसी तरह आगे और आगे।एक साथ लाइन द्वारा दो टेक्स्टफाइल लाइन पढ़ें-python

क्या फ़ाइल लाइन-दर-रेखा दोनों को एक साथ पढ़ने का कोई तरीका है?

नीचे एक नमूना है कि फ़ाइलों को कैसा दिखना चाहिए, कल्पना करें कि प्रति फ़ाइल लाइनों की संख्या लगभग 1,000,000 है।

textfile1:

This is a the first line in English 
This is a the 2nd line in English 
This is a the third line in English 

textfile2:

C'est la première ligne en Français 
C'est la deuxième ligne en Français 
C'est la troisième ligne en Français 

वांछित आउटपुट

This is a the first line in English\tC'est la première ligne en Français 
This is a the 2nd line in English\tC'est la deuxième ligne en Français 
This is a the third line in English\tC'est la troisième ligne en Français 

इस Read two textfile line by line simultaneously -java की एक जावा संस्करण नहीं है, लेकिन अजगर का उपयोग नहीं करता bufferedreader कि लाइन पढ़ता रेखा से। तो यह कैसे किया जाएगा?

+3

यह अजगर नहीं है, लेकिन अगर आप सिर्फ एक नई फ़ाइल में उत्पादन की जरूरत है, 'पेस्ट textfile1 textfile2> output':

wrt एक बार में दो iterables से अधिक/बार-बार दोहराना, itertools.izip अपने दोस्त है भी काम करना चाहिए। – eumiro

+0

यदि आपको लार्सन के उत्तर पसंद हैं, तो आप इसे स्वीकार किए जाने के रूप में चिह्नित करना चाहेंगे। –

उत्तर

64
from itertools import izip 

with open("textfile1") as textfile1, open("textfile2") as textfile2: 
    for x, y in izip(textfile1, textfile2): 
     x = x.strip() 
     y = y.strip() 
     print("{0}\t{1}".format(x, y)) 

अजगर 3 में, साथ itertools.izip की जगह में निर्मित zip

+0

धन्यवाद, यह एक आकर्षण – alvas

+0

की तरह काम किया मैं इस मुद्दे मिलती है: फ़ाइल "MergeANNOVARResults.py", लाइन 10 खुला (RefSeq) के साथ refseq_fh, खुले (gencode) gencode_fh के रूप में के रूप में: ^ सिंटैक्स त्रुटि: अमान्य वाक्य रचना – jmtoung

13
with open(file1) as f1, open(fil2) as f2: 
    for x, y in zip(f1, f2): 
    print("{0}\t{1}".format(x.strip(), y.strip())) 

उत्पादन:

This is a the first line in English C'est la première ligne en Français 
This is a the 2nd line in English C'est la deuxième ligne en Français 
This is a the third line in English C'est la troisième ligne en Français 
+2

ध्यान रखें कि ज़िप() दोनों फ़ाइलों की पूर्ण सामग्री को स्मृति में (पायथन 2.x में) –

+0

बेहतर उपयोग ['itertools आयात izip'] से बेहतर उपयोग करेगा (http://docs.python.org/library/itertools। एचटीएमएल # itertools.izip) या ['izip_longest'] (http://docs.python.org/library/itertools.html#itertools.izip_longest)। हालांकि 'साथ' के उपयोग के लिए –

+2

+1। –

3

पायथन आपको लाइन से लाइन पढ़ने देता है, और यह भी डिफ़ॉल्ट व्यवहार है - आप फ़ाइल पर फिर से सक्रिय होते हैं जैसे कि सूची में पुनरावृत्ति होती है।

from itertools import izip 
fileA = open("/path/to/file1") 
fileB = open("/path/to/file2") 
for lineA, lineB in izip(fileA, fileB): 
    print "%s\t%s" % (lineA.rstrip(), lineB.rstrip()) 
संबंधित मुद्दे