Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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中逐步迭代2个文件_Python_Loops_Readlines - Fatal编程技术网

尝试在python中逐步迭代2个文件

尝试在python中逐步迭代2个文件,python,loops,readlines,Python,Loops,Readlines,我正在尝试将两个大的输入文件合并到一个输出中,一边进行排序 ## Above I counted the number of lines in each table print("Processing Table Lines: table 1 has " + str(count1) + " and table 2 has " + str(count2) ) newLine, compare, line1, line2 = [], 0, [], [] while count1 + count2

我正在尝试将两个大的输入文件合并到一个输出中,一边进行排序

## Above I counted the number of lines in each table

print("Processing Table Lines: table 1 has " + str(count1) + " and table 2 has " + str(count2) )
newLine, compare, line1, line2 = [], 0, [], []

while count1 + count2 > 0:
    if count1 > 0 and compare <= 0: count1, line1 = count1 - 1, ifh1.readline().rstrip().split('\t')
    else: line1 = []
    if count2 > 0 and compare >= 0: count2, line2 = count2 - 1, ifh2.readline().rstrip().split('\t')
    else: line2 = []

    compare = compareTableLines( line1, line2 )
    newLine = mergeLines( line1, line2, compare, tIndexes )

    ofh.write('\t'.join( newLine + '\n'))
###以上我计算了每个表中的行数
打印(“处理表行:表1有“+str(count1)+”,表2有“+str(count2))
换行符,比较,第1行,第2行=[],0,[],[]
当count1+count2>0时:
如果count1>0,比较0,比较>=0:count2,line2=count2-1,ifh2.readline().rstrip().split('\t')
其他:第2行=[]
compare=compareTableLines(第1行,第2行)
换行符=合并行(行1、行2、比较、索引)
写入('\t'.join(换行符+'\n'))
我期望发生的是,在将行写入输出时,如果可用的话,我会提取文件中的下一行。我还希望两个文件都为空时,循环将被切断

然而,我不断地得到这个错误: ValueError:混合迭代和读取方法将丢失数据


我就是不知道该怎么绕开它。这两个文件都太大,无法保存在内存中,因此我希望边读边读。

下面是一个合并两个有序文件的示例,在本例中是CSV文件,使用and。给定2个CSV文件:

x.csv

key1,99
key2,100
key4,234
key1,345
key2,4
key3,45
key1,444
key2,104
key3,45
key4,234

y.csv

key1,99
key2,100
key4,234
key1,345
key2,4
key3,45
key1,444
key2,104
key3,45
key4,234

运行:

导入csv、heapq、itertools
keyfun=lambda行:行[0]
打开(“x.csv”)作为inf1,打开(“y.csv”)作为inf2,打开(“z.csv”,“w”)作为输出:
in1,in2,out=csv.reader(inf1),csv.reader(inf2),csv.writer(outf)
对于键,itertools.groupby(heapq.merge(in1,in2,key=keyfun),keyfun)中的行:
out.writerow([key,sum(int(r[1])表示行中的r)])
我们得到:

z.csv

key1,99
key2,100
key4,234
key1,345
key2,4
key3,45
key1,444
key2,104
key3,45
key4,234


这两个输入文件是否已排序?你能给我们举几个例子吗?
compareTableLines()
做什么?