我有两个文件file1
和file2
,每个文件包含多个数字(每行一个数字)。我需要创建一个第三个文件(output file
),它将两个文件组合在一起而不会有任何重复的数字。在文件中组合两个文件的代码应该是什么?
File1: 1 2 7 9 15 (1 number per line)
File2: 1 8 12 13 14 16 (1 number per line)
Outputfile: 1 2 7 8 9 12 13 14 15 16 (1 number per line)
答案 0 :(得分:1)
假设您的输入文件位于当前工作目录中:
unique_elements = set()
for filename in ['file1', 'file2']:
with open(filename, 'r') as f:
for l in f.readlines():
unique_elements.add(int(l.strip()))
sorted_list = list(unique_elements)
sorted_list.sort()
with open('output_file', 'w') as f:
for number in sorted_list:
f.write('{}\n'.format(number))
答案 1 :(得分:0)
因此,如果订单很重要,您可以简单地将两者都读入一个列表然后写一个最终的一个,类似于:
numbers = []
with open(file1, 'r') as inputfile:
for row in inputfile.readlines():
number = int(row)
if number not in numbers:
numbers.append(number)
with open(file2, 'r') as inputfile:
for row in inputfile.readlines():
number = int(row)
if number not in numbers:
numbers.append(number)
numbers.sort()
with open(file3, 'w') as outputfile:
for number in numbers:
outputfile.write("%d\n" % number)