【发布时间】:2012-07-02 22:56:20
【问题描述】:
我有两个不同语言的文本文件,它们逐行对齐。 IE。 textfile1 中的第一行对应于 textfile2 中的第一行,以此类推。
有没有办法同时逐行读取两个文件?
下面是文件的样例,假设每个文件的行数约为 1,000,000。
文本文件1:
This is a the first line in English
This is a the 2nd line in English
This is a the third line in English
文本文件2:
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 有一个Java 版本,但是Python 不使用逐行读取的bufferedreader。那么如何实现呢?
【问题讨论】:
-
这不是 Python,但如果您只需要在新文件中输出,
paste textfile1 textfile2 > output也应该可以。 -
如果您喜欢 larsmans 的回答,您可能希望将其标记为已接受。