Ordered file combination with python -
i have 2 files, file1
, file2
, each 1 containing several numbers (one number per line). need create third file (output file
) combines both files without having repeated number. should code combining both files in 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)
assuming input files in current working directory:
unique_elements = set() filename in ['file1', 'file2']: open(filename, 'r') f: l in f.readlines(): unique_elements.add(int(l.strip())) sorted_list = list(unique_elements) sorted_list.sort() open('output_file', 'w') f: number in sorted_list: f.write('{}\n'.format(number))
Comments
Post a Comment