|
3 | 3 | from json import loads, dumps
|
4 | 4 | from sys import argv
|
5 | 5 |
|
6 |
| -xmlPath = "" |
7 |
| -csvPath = "" |
8 | 6 |
|
9 | 7 | # validated the ammout of args passed from the command line, so I could get the path from both files before starting the script
|
| 8 | +def getInputAndOutputFiles(): |
| 9 | + if len(argv) == 1: |
| 10 | + xmlPath = input("please enter the path to your JSON file: ") |
| 11 | + csvPath = input("please enter the path to your YAML file: ") |
| 12 | + elif len(argv) == 2: |
| 13 | + xmlPath = argv[1] |
| 14 | + csvPath = input("please enter the path to your YAML file: ") |
| 15 | + elif len(argv) >= 3: |
| 16 | + xmlPath = argv[1] |
| 17 | + csvPath = argv[2] |
| 18 | + return xmlPath, csvPath |
10 | 19 |
|
11 |
| -if len(argv) == 1: |
12 |
| - xmlPath = input("please enter the path to your XML file: ") |
13 |
| - csvPath = input("please enter the path to your CSV file: ") |
14 |
| -elif len(argv) == 2: |
15 |
| - xmlPath = argv[1] |
16 |
| - csvPath = input("please enter the path to your CSV file: ") |
17 |
| -elif len(argv) >= 3: |
18 |
| - xmlPath = argv[1] |
19 |
| - csvPath = argv[2] |
| 20 | + |
| 21 | +xmlPath, csvPath = getInputAndOutputFiles() |
20 | 22 |
|
21 | 23 | print("started to convert your file...")
|
22 | 24 |
|
23 |
| -# from line 29 to 33 |
| 25 | +# from line 31 to 35 |
24 | 26 | # I read the file from the xmlPath variable, and got the dictory from parseXML
|
25 | 27 | # After that I got the first key from the dict (asumming the user passed a single xml liked the test.xml file)
|
26 | 28 | # After that I got the list of nodes inside the "root"
|
27 | 29 | # And finally I close the file
|
28 |
| - |
29 |
| -xmlFile = open(xmlPath, "r") |
30 |
| -data_dict = parseXML(xmlFile.read()) |
31 |
| -data_root = data_dict[list(data_dict.keys())[0]] |
32 |
| -itemsList = list(data_root.values())[0] |
33 |
| -xmlFile.close() |
34 |
| - |
| 30 | +def getChildNodesFromRoot(): |
| 31 | + with open(xmlPath, "r") as xmlFile: |
| 32 | + data_dict = parseXML(xmlFile.read()) |
| 33 | + data_root = data_dict[list(data_dict.keys())[0]] |
| 34 | + itemsList = list(data_root.values())[0] |
| 35 | + return itemsList |
| 36 | + |
| 37 | +itemsList = getChildNodesFromRoot() |
35 | 38 | # Declared a csvWrite (object to write rows on a csv file)
|
36 | 39 | csvFile = open(csvPath, "w")
|
37 | 40 | csvWriter = CSVWriter(csvFile)
|
38 | 41 |
|
39 | 42 |
|
40 |
| -# from line 46 to 53 |
| 43 | +# from line 49 to 57 |
41 | 44 | # I iterated over all the items on the child of the file root
|
42 | 45 | # After that i made the variable item into a real dict so I could use
|
43 | 46 | # .keys and .values, the keys for the header row
|
|
0 commit comments