-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgen_csv_arg.py
More file actions
executable file
·51 lines (40 loc) · 1.31 KB
/
gen_csv_arg.py
File metadata and controls
executable file
·51 lines (40 loc) · 1.31 KB
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
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import argparse
import pandas as pd
import xml.etree.ElementTree as ET
from icecream import ic
from pathlib import Path
# verbose icecream
ic.configureOutput(includeContext=True)
# argparse
parser = argparse.ArgumentParser(description='Generate CSV file from XML file.')
parser.add_argument('-i', '--input', help='Input XML file', required=False)
parser.add_argument('-o', '--output', help='Output CSV file', required=False)
args = parser.parse_args()
# get xml_id's
if args.input is not None:
xml = args.input
else:
xml_cwd = list(Path("raw").glob("*.xml"))
xml = xml_cwd[0].name
root = ET.parse(xml)
id_list = []
name_list = []
for id in root.findall(".//id"):
xml_id = id.text
id_list.append(xml_id)
for name in root.findall(".//name"):
xml_name = name.text
name_list.append(xml_name)
zipped = zip(id_list, name_list)
zipped = sorted(zipped, key=lambda col: col[1].casefold())
ic(list(zipped))
# zip lists
df = pd.DataFrame(list(zipped), columns=["id", "name"])
# export zipped list as csv with pandas
if args.output is None and not Path('output.csv').is_file():
df.to_csv("output.csv", index=False)
elif args.output is None and Path('output.csv').is_file():
df.to_csv("output.csv", index=False, mode='a', header=False)
else:
df.to_csv(args.output, index=False)