2

I'm new to Python but I have an GET Request using requests which is showing me what I want to see, however, how am I able to export it out to a column in CSV?

import requests
url = '<URL>'
response = requests.get(url, verify=False, auth = ('username', 'pass'))
jsonResponse = response.json()
print("NAME")
print(jsonResponse["limits"]['assets'])

The response is: NAME Number (As Assets is nested)

Ideally I'd like to show both NAME and Number in different columns: NAME | Number

My plan is to have multiple requests in the one script which will then spit out a CSV file with the data I'm trying to capture

baz
  • 23
  • 2

2 Answers2

0

Maybe I'm using an atomic bomb to kill a fly, but I would consider to build the whole data structure into a pandas dataframe and then export it. Check here to transform json to df and here to export to csv

Roberto
  • 86
  • 1
  • 7
0

You can use the CSV module

import csv

with open('test.csv', mode='a') as file:
    w = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    w.writerow(['NAME', jsonResponse["limits"]['assets']])
Ammar Aslam
  • 560
  • 2
  • 16