-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvFormatConverter.py
35 lines (29 loc) · 1.06 KB
/
csvFormatConverter.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
import pandas as pd
import sys, getopt
# this script flips files from the ['from', 'to'] format
# to ['to', 'from'] format and the other way around
def main(argv):
input_file = ''
output_file = ''
try:
opts, args = getopt.getopt(argv, "hi:o:", ["input=", "output="])
except getopt.GetoptError:
print('python3 csvFormatConverter.py -i <input_file> -o <output_file>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print("use --input and --output")
sys.exit()
elif opt in ("-i", "--input"):
input_file = arg
elif opt in ("-o", "--output"):
output_file = arg
if input_file == '' or output_file == '':
print('python3 csvFormatConverter.py -i <input_file> -o <output_file>')
sys.exit(2)
df = pd.read_csv(input_file, sep=",", names=["from", "to"])
df = df.reindex(columns={"to": "from", "from": "to"})
df.to_csv(output_file, index=False, header=False)
if __name__ == "__main__":
if len(sys.argv) > 1:
main(sys.argv[1:])