【问题标题】:Compare 2 lists in file and save result in another file比较文件中的 2 个列表并将结果保存在另一个文件中
【发布时间】:2021-11-10 11:18:08
【问题描述】:

我在文件中记录了 2 个列表(File1.txt 和 File2.txt),我需要逐行比较并在 Output.txt 文件中记录相等出现的数量。

但是,写入输出的结果不正确。请参阅下面我使用的代码以及获得的结果和期望的结果:

input_file1 = open('C:\\Temp\\File1.txt', 'r')
input_file2 = open('C:\\Temp\\File2.txt', 'r')
output_file = open('C:\\Temp\\Output.txt','w')

match = 0
strOutput = ""

for line1 in input_file1:
    LST1 = list(line1)
    input_file2.seek(0)
    output_file.write('\n')
    for line2 in input_file2:
        LST2 = list(line2)
        match = len(set(LST1).intersection(set(LST2)))
        strOutput = str(match) + ','  + line2
        output_file.write("%s" %(strOutput))
                        
output_file.close()
input_file2.close()
input_file1.close()

input_file1:
01,04,07,23,39
03,05,08,37,45
02,03,10,13,28

input_file2:
01,02,03,21,22,23,27
03,05,10,13,37,39,47

输出(不正确!):
7,01,02,03,21,22,23,27
7,03,05,10,13,37,39,47
5,01,02,03,21,22,23,27
6,03,05,10,13,37,39,47
5,01,02,03,21,22,23,27
4,03,05,10,13,37,39,47

输出(正确):
2,01,02,03,21,22,23,27
1,03,05,10,13,37,39,47
1,01,02,03,21,22,23,27
3,03,05,10,13,37,39,47
2,01,02,03,21,22,23,27
3,03,05,10,13,37,39,47

或:

输出(正确):
2,1,2,3,21,22,23,27
1,3,5,10,13,37,39,47
1,1,2,3,21,22,23,27
3,3,5,10,13,37,39,47
2,1,2,3,21,22,23,27
3,3,5,10,13,37,39,47

【问题讨论】:

  • 您是否尝试将项目拆分为 [01 , 02 , 03, ...] 中的列表?因为在您当前的代码中,它似乎正在生成一个列表,如下所示: [ '0' , '1' , ',' , '0' , ....] 这可能是问题所在。
  • @zaid-al-shattle - 真的,这是问题之一。但是,如果您在输入文件(File1.txt 和 File2.txt)中使用 1,2,3(整数转换),......它也不会给出所需的输出。
  • 给我几分钟,我可以尝试为您解决问题。
  • @J.Silva 您需要解析输入行,如下所示:LST1 = line1.strip().split(',')LST1 = list(map(int, line1.strip().split(',')))
  • @ekhumoro - 恭喜。感谢您的代码。我也喜欢 zaid-al-shattle 的解决方案。两者都有效。

标签: python list csv


【解决方案1】:

这里是固定代码:

input_file1 = open('File1.txt', 'r')
input_file2 = open('File2.txt', 'r')
output_file = open('Output.txt','w')

match = 0
strOutput = ""

for line1 in input_file1:
    LST1 = line1.strip().split(',')
    input_file2.seek(0)
    output_file.write('\n')
    for line2 in input_file2:
        LST2 = line2.strip().split(',')
        match = len(set(LST1).intersection(set(LST2)))
        strOutput = str(match) + ','  + line2
        output_file.write("%s" %(strOutput))
                        
output_file.close()
input_file2.close()
input_file1.close()

主要有两个问题:

1- 原行:LST1 = list(line1) 没有生成正确的列表,以列表结尾:['0' , '1' , ',' , '0' , ....] 而不是 ['01','02',...]

2- 在您的原始文档中,每行末尾都有一个换行符,因此您的 line1 如下所示:'01,04,07,23,39\n' 为了解决这个问题,我们删除了最后 2 个字符。

通过这两项更改,您将得到以下代码:

LST1 = line1.strip().split(',')

.strip() 删除换行符,.split(',') 正确拆分它。

运行该代码给了我这个输出:

2,01,02,03,21,22,23,27
1,03,05,10,13,37,39,47
1,01,02,03,21,22,23,27
3,03,05,10,13,37,39,47
2,01,02,03,21,22,23,27
3,03,05,10,13,37,39,47

【讨论】:

  • 您的第一个解决方案很容易出错,因为它假定每一行都必须以换行符结尾。使用strip() 删除任何尾随空格会更加健壮。
  • 我注意到了这一点,因此我进行了编辑。您是否建议一起删除第一个选项并将其替换为已编辑的答案? @ekhumoro?
  • @zaid-al-shattle - 恭喜。感谢您的解释。我也喜欢 ekhumoro 的解决方案。两者都有效。
  • @ZaidAlShatle 我认为是这样,因为它会给出错误的输出。 (此外,它实际上是删除 一个 字符,而不是两个)。
  • 已记录,现在将对其进行编辑。我在写两个字符时犯了一个错误(因为 /n 被认为是一个字符)。感谢您的反馈!
猜你喜欢
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
相关资源
最近更新 更多