-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-spaces.py
43 lines (30 loc) · 854 Bytes
/
remove-spaces.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
35
36
37
38
39
40
41
42
43
#!/usr/local/bin/ python
import re
import csv
import argparse
def arguments():
"""
Parse command line arguments
@return filename (string)
"""
parser = argparse.ArgumentParser(description="txt file location.")
parser.add_argument("-f", "--file", default="", help="Path of the txt file.", required=True)
args = parser.parse_args()
return args.file
def main(filename):
"""
The main function
"""
filename_csv = filename.replace('.txt', '.csv')
# Create csv file
csv_data = open(filename_csv, 'w')
# Open txt file
with open(filename, 'r') as f:
for line in f:
line = re.sub(',', '', line)
new_line = re.sub('\s\s+', ',', line)
csv_data.write(new_line)
f.close()
if __name__ == "__main__":
filename = arguments()
main(filename)