-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_csv.py
executable file
·34 lines (26 loc) · 943 Bytes
/
clean_csv.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
32
33
34
#!/usr/bin/env python3
import csv
import sys
def remove_quotes_from_content(content):
return content.replace('"', '')
def clean_csv(input_file, output_file):
with open(input_file, 'r', newline='') as f:
content = f.read()
content = remove_quotes_from_content(content)
lines = list(csv.reader(content.splitlines()))
cleaned_lines = []
for line in lines:
# Assuming maximum 2 fields per line (as per your example)
while len(line) > 2:
line[0] = line[0] + ',' + line.pop(1)
cleaned_lines.append(line)
with open(output_file, 'w', newline='\n') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(cleaned_lines)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: script_name input_file output_file")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
clean_csv(input_file, output_file)