2012-07-02 14 views
32

Mam 2 pliki tekstowe w dwóch różnych językach i są one wyrównane linia po linii. To znaczy. pierwsza linia w pliku tekstowym 1 powinna być równa pierwszej linii w pliku tekstowym2 i tak dalej.Czytaj dwie linie tekstu po linii jednocześnie -python

Czy istnieje sposób jednoczesnego odczytu obu plików wiersz po linii?

Poniżej znajduje się przykład pokazujący, jak powinny wyglądać pliki. Wyobraź sobie, że liczba wierszy na plik wynosi około 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 

pożądane wyjście

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 

Istnieje wersja java tej Read two textfile line by line simultaneously -java, ale pyton nie używa BufferedReader, który odczytuje linię przez linię. Jak więc można to zrobić?

+3

To nie jest Python, ale jeśli wystarczy wyjście w nowym pliku, 'wklej textfile1 textfile2> output' też powinien działać. – eumiro

+0

Jeśli podoba ci się odpowiedź larsmansa, możesz ją oznaczyć jako zaakceptowaną. –

Odpowiedz

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)) 

W Pythonie 3, wymienić itertools.izip z wbudowaną zip.

+0

Dzięki, to działało jak czar – alvas

+0

pojawia się problem: Plik "MergeANNOVARResults.py", linia 10 z otwartą (REFSEQ) jako refseq_fh, otwarte (gencode) jako gencode_fh: ^ SyntaxError: nieprawidłową składnię – 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())) 

wyjściowa:

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

Należy pamiętać, że zip() pobierze pełną zawartość obu plików do pamięci (w języku Python 2.x). –

+0

Lepsze użycie ['from itertools import izip'] (http://docs.python.org/library/itertools. html # itertools.izip) lub ['izip_longest'] (http://docs.python.org/library/itertools.html#itertools.izip_longest). –

+2

+1 za użycie 'with' chociaż. –

3

Python umożliwia czytanie linii po linii, a nawet zachowanie domyślne - po prostu następuje iteracja po pliku, tak jak w przypadku iteracji na liście.

wrt/Iteracja nad dwoma iterables jednocześnie, itertools.izip jest twoim przyjacielem:

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()) 
Powiązane problemy