abjer / sds2019

Social Data Science 2019 - a summer school course
https://abjer.github.io/sds2019
46 stars 96 forks source link

Saving API CSV-response in a JSON-file #17

Open Choptdei opened 5 years ago

Choptdei commented 5 years ago

Hi, we talked with one of the TA's who advised us to chose CSV as the format when choosing the settings of the GET-call in the Danmarks Statisitik API. (JSON was not in the list of available settings). How do we transform the CSV-format to JSON format, which is necessary to complete 3.3.2? Or should we change the link-constructor in 3.3.1?

First, we created the function, that gets the link for specific parametres (sorry, its not perfect):

def construct_link(table_id, lst): url = "https://api.statbank.dk/v1/data/" + table_id + "/CSV?" for element in lst: url += element + "&" return url[:-1]

This is the code for 3.2.2, where we with no sucess tried to use the requests module on csv format:

`import requests import csv

response = requests.get(construct_link("FOD",['tid=*',"barnkon=p"])) response_csv = response.csv()

with open('my_csv.csv', 'w') as csv: csv.write(response_csv)`

elben10 commented 5 years ago

If you choose the csv format you will not be able to save the file as a json file. The idea behind the exercise is to retrieve a json file, and use the json library to save the file to your file system

kristianolesenlarsen commented 5 years ago

Simply change the "CSV?" to "JSONSTAT?".

Choptdei commented 5 years ago

Since we were told to get CSV, we thought that the JSONSTAT was not suitable - just like Java and Javascript is not the same. We will use JSONSTAT instead now. Thanks