From aa9890e5b582cb7ff615c67fe1fea0f29bdbc9db Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Fri, 6 Dec 2019 13:04:52 -0500 Subject: [PATCH] add script to convert data.json to a TSV file #5 --- .gitignore | 1 + json2tsv.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 .gitignore create mode 100755 json2tsv.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bbe530 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dataverse-installations.tsv diff --git a/json2tsv.py b/json2tsv.py new file mode 100755 index 0000000..09097f5 --- /dev/null +++ b/json2tsv.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +import json +import csv +with open('data/data.json', 'r') as json_file: + json_data=json_file.read() +installations = json.loads(json_data)['installations'] +with open('dataverse-installations.tsv', 'w', newline='') as tsvfile: + output = csv.writer(tsvfile, delimiter='\t') + header = [ + 'name', + 'country', + 'launch_year', + 'hostname', + 'continent', + 'latitude', + 'longitude', + 'about_url', + 'description', + ] + output.writerow(header) + for i in installations: + name = i['name'] + country = i['country'] + launch_year = i.get('launch_year', None) + continent = i['continent'] + hostname = i['hostname'] + latitude = i['lat'] + longitude = i['lng'] + about_url = i.get('about_url', None) + description = i['description'] + output.writerow([ + name, + country, + launch_year, + hostname, + continent, + latitude, + longitude, + about_url, + description, + ])