Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按顺序跨两个文件中的行进行迭代_Python - Fatal编程技术网

Python 按顺序跨两个文件中的行进行迭代

Python 按顺序跨两个文件中的行进行迭代,python,Python,我有两个文件,我想在这两个文件之间执行一些行操作(一个接一个)。我现在使用两个循环来实现这一点。是否有一种方法可以在单个循环中完成(在python 2.7中): 模块有一个工具来实现这一点。试试这个: import itertools for line in itertools.chain(open(file_name1), open(file_name2)): # do something 也许您可以使用列表理解或映射、减少、过滤等来避免内部循环。这一切都取决于您的需要。您想做什么

我有两个文件,我想在这两个文件之间执行一些行操作(一个接一个)。我现在使用两个循环来实现这一点。是否有一种方法可以在单个循环中完成(在python 2.7中):

模块有一个工具来实现这一点。试试这个:

import itertools

for line in itertools.chain(open(file_name1), open(file_name2)):
    # do something

也许您可以使用列表理解或映射、减少、过滤等来避免内部循环。这一切都取决于您的需要。您想做什么?

不完全是您想要的,但是文件输入模块可能很有用

"""Helper class to quickly write a loop over all standard input files. Typical use is: import fileinput for line in fileinput.input(): process(line) This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-' it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the argument to input(). A single file name is also allowed. Functions filename(), lineno() return the filename and cumulative line number of the line that has just been read; filelineno() returns its line number in the current file; isfirstline() returns true iff the line just read is the first line of its file; isstdin() returns true iff the line was read from sys.stdin. Function nextfile() closes the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count; the filename is not changed until after the first line of the next file has been read. Function close() closes the sequence. ... “”“Helper类,用于在所有标准输入文件上快速编写循环。 典型用途是: 导入文件输入 对于fileinput.input()中的行: 工艺(生产线) 这将迭代sys.argv[1:]中列出的所有文件的行, 如果列表为空,则默认为sys.stdin。如果文件名为“-”,则为 也被sys.stdin.替换,以指定 文件名,将其作为参数传递给input()。单个文件名为 也允许。 函数filename(),lineno()返回文件名和累计行 刚读取的行数;filelineno()返回其 当前文件中的行号;isfirstline()在 刚读取的行是其文件的第一行;isstdin()返回true 如果从sys.stdin读取该行,函数nextfile()将关闭 当前文件,以便下一次迭代将从 下一个文件(如果有);未从文件中读取的行将不计算在内 朝向累计行数;文件名直到 读取下一个文件的第一行后。函数close() 关闭序列。 ...
正如已经指出的那样,
itertools.chain
是一个选项,但是还有另一个有用的标准模块,它避免了显式使用
open

import fileinput
for line in fileinput.input(['file1.txt', 'file2.txt']):
    print line
这也有一些方便的行号和文件名等功能。请查看

回复评论-与上下文管理器一起使用

from contextlib import closing

with closing(fileinput.input(['file1.txt', 'file2.txt'])) as infiles:
    for line in infiles:
        pass # stuff

这是我的第一个答案,所以请为任何错误道歉

>>> file1 = open('H:\\file-1.txt')
>>> file2 = open('H:\\file-2.txt')
>>> for i, j in map(None, file1.readlines(), file2.readlines()):
        print i,j

+1我更喜欢这个,因为
itertools
解决方案会使关闭文件变得不方便。如果程序突然结束,下次运行它时,我会得到一个
RuntimeError:input()已经激活了。有没有办法绕过这个问题。“是的,我现在正在这样做。有没有办法在代码中实现这一点,这样每当它中断文件就关闭了。谢谢。这些解决方案仍然涉及循环。我会认为这更多的是一个注释而不是答案。虽然您的解决方案完成了任务,但我是CHOOS。查看
fileinput
解决方案,因为它在读取文件后会关闭该文件。无论如何,谢谢。-1不是问题。另外
zip(*args)
map(None,*args)
。不需要
readlines
文件
对象是文件行中的迭代器,因此您可以简单地执行:
zip(file1,file2)
>>> file1 = open('H:\\file-1.txt')
>>> file2 = open('H:\\file-2.txt')
>>> for i, j in map(None, file1.readlines(), file2.readlines()):
        print i,j