1

I have a really simple question I want to export the output for this code into csv file, what should I add as it shows the result printed to the terminal (json) and I want the results to be csv files


import requests
import pandas as pd
import numpy as np
import json

payload = {}
headers = {}

CLIENT_ID = "****"
CLIENT_SECRET = "****"
TOKEN = "****"


ll1 = "18.036442,42.492103"
ll2 = "18.036808,42.117917"
ll3 = "18.458814,42.485903"
ll4 = "18.453989,42.115408"


def make_request(payload, headers, ll1, CLIENT_ID,CLIENT_SECRET ):
   url = f"https://api.foursquare.com/v2/venues/search" \
   "?client_id={CLIENT_ID}" \
   "&client_secret={CLIENT_SECRECT}" \
   "&token={TOKEN}" \
   "&ll={ll1}" \
   "&intent=browse&radius=10000&v=20210927"

   response = requests.request("GET", url, headers=headers, data=payload)
   return response.json()


data = make_request(payload, headers, ll1,CLIENT_ID, CLIENT_SECRET)
data = make_request(payload, headers, ll2,CLIENT_ID, CLIENT_SECRET)
data = make_request(payload, headers, ll3,CLIENT_ID, CLIENT_SECRET)
data = make_request(payload, headers, ll4,CLIENT_ID, CLIENT_SECRET)


response_api = requests.get(
   data
)

print (data)

data = response_api.json()

Thank you!

hestaru
  • 31
  • 3
  • 1
    Please, edit the title - export to excel is misleading if in fact you want to export csv. – buran Nov 04 '21 at 21:36
  • first you should show what you have in `data`. Maybe you should first format data in 2D list - list of rows - and later it should be simple to use module `csv` or `pandas`. – furas Nov 04 '21 at 23:29
  • Does this answer your question? [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – Zach Young Nov 04 '21 at 23:33

1 Answers1

1

You can use this site. or you can use this code:

json_file = {'name':["aparna", "pankaj", "sudhir", "Geeku"],'degree': ["MBA", "BCA", "M.Tech", "MBA"],'score':[90, 40, 80, 98]}
df = pd.DataFrame(json_file).to_excel("excel.xlsx")
cvsrt
  • 357
  • 4
  • 19