-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: align columns in csv with json
- Loading branch information
Showing
5 changed files
with
39 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import csv | ||
import json | ||
|
||
def csv_to_json(csvFilePath, jsonFilePath): | ||
data = {} | ||
data["label"] = "Recommendation Code List 20" | ||
data["uri"] = "rec20" | ||
data["comment"] = "CODES FOR UNITS OF MEASURE USED IN INTERNATIONAL TRADE" | ||
values = [] | ||
|
||
#read csv file | ||
with open(csvFilePath, encoding='utf-8') as csvf: | ||
#load csv file data using csv library's dictionary reader | ||
csvReader = csv.DictReader(csvf) | ||
|
||
#convert each csv row into python dict | ||
for row in csvReader: | ||
#add this python dict to json array | ||
row["uri"] = "unece:rec20#"+row["CommonCode"] | ||
values.append(row) | ||
|
||
data["values"] = values | ||
data["referencedBy"] = [] | ||
|
||
#convert python jsonArray to JSON String and write to file | ||
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf: | ||
jsonString = json.dumps(data, indent=4) | ||
jsonf.write(jsonString) | ||
|
||
csvFilePath = r'current/Annex || & Annex |||/code-list.csv' | ||
jsonFilePath = r'current/rec20.json' | ||
csv_to_json(csvFilePath, jsonFilePath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters