-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseparator.py
32 lines (23 loc) · 859 Bytes
/
separator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# with open('predictions-best.txt', 'r') as file:
# lines = file.readlines()
# files = {'0': open('file_A.txt', 'w'), '1': open('file_B.txt', 'w'), '2': open('file_C.txt', 'w')}
# for line in lines:
# words = line.split()
# if len(words) > 1:
# key = words[1]
# files[key].write(line)
# # Close all files
# for file in files.values():
# file.close()
# Specify your input and output file names
input_file = 'file_C.txt'
output_file = 'sorted_label2.txt'
# Read lines from the input file
with open(input_file, 'r') as file:
lines = file.readlines()
# Sort lines by the first word of each line
lines.sort(key=lambda x: x.split()[0])
# Write the sorted lines to the output file
with open(output_file, 'w') as file:
file.writelines(lines)
print(f"File '{input_file}' has been sorted and saved to '{output_file}'.")