python加入2个文件并写入新文件

时间:2020-08-06 14:45:55

标签: python python-3.x

我有如下要求:

input1_txt

a
b
c

input2_txt

1|2|3|a|e|f
1|3|4|b|g|h
3|2|4|c|f|h
d|f|g|i|h|j

我正在尝试同时读取input1,input2并写入ouput_file

如果input1第一列等于input2第四列 然后将所有输入2写入ouput_file。

代码:

read1 = csv.reader(open(input1_txt, 'r')) # read input file 1
write = csv.writer(open(output_file,"w"), delimiter="|", lineterminator="\n")  
 

for row1 in read1:
    #print(row1)
    read2 = csv.reader(open(input2_file, 'r'), delimiter="|", lineterminator="\n")
    for row2 in read2:
        #print(row2)
        if row1 == row2[4]:
            write.writerows(row2)

预期输出如下:即仅匹配input1第一列和input2第四列

                  1|2|3|a|e|f
                  1|3|4|b|g|h
                  3|2|4|c|f|h

此代码显示了如何无法获得任何结果。请告知。

2 个答案:

答案 0 :(得分:0)

import csv

read1 = csv.reader(open("input1.txt","r"))
read2 = csv.reader(open("input2.txt","r"),delimiter="|")

writter = csv.writer(open("output_file","w"),delimiter="|")

# this is needed because the csv iterator doesn't reset
all_row2 = [x for x in read2]

all_rows = []
for row1 in read1:
    # every row is a list
    # if you want a specific column you can do : if row1[0] in x[4]
    # you get a list of rows   
    all_rows.extend([x for x in all_row2 if row1[0] in x])
# write all at once .. faster
writter.writerows(all_rows)

答案 1 :(得分:0)

您的索引错误

    if row1[0] == row2[3]:

这就是你所有的问题


顺便说一句::下次第一次使用print()来查看变量中的内容时-有助于查看问题。


最少的工作代码。

我仅使用io.StringIO来模拟文件-这样每个人都可以运行它。

data1 = '''a
b
c'''

data2 = '''1|2|3|a|e|f
1|3|4|b|g|h
3|2|4|c|f|h
d|f|g|i|h|j'''

import csv
import io

read1 = csv.reader(io.StringIO(data1)) # read input file 1
#read1 = csv.reader(open(input1_txt, 'r')) # read input file 1
#write = csv.writer(open(output_file,"w"), delimiter="|", lineterminator="\n")  
 

for row1 in read1:
    read2 = csv.reader(io.StringIO(data2), delimiter="|", lineterminator="\n")
    #read2 = csv.reader(open(input2_file, 'r'), delimiter="|", lineterminator="\n")
    for row2 in read2:
        if row1[0] == row2[3]:
            print('|'.join(row2))
            #write.writerows(row2)
相关问题