简体   繁体   中英

Converting output from API to CSV

I am trying to use an API for a platform called "ParseHub". They have sample code to use it in Python but unfortunately I am not the best in Python and I cannot figure out how to save the file as a CSV... Here is the code:

import requests

params = {
  "api_key": "tSKqprcg7k-S",
  "format": "csv"
  }
r = requests.get('https://www.parsehub.com/api/v2/projects/tq1H9ha1sKAQ/last_ready_run/data', params=params)
print(r.text)

How can I save the output as a CSV or excel file?

Thank you!

Edit: Here is what the output looks like. It's just a ton of rows like this

"132","Adult","2","2018-03-22","99","38","2"
"151","Adult","2","2018-03-23","99","30","2"
"152","Adult","2","2018-03-24","99","29","2"
"138","Adult","2","2018-03-25","99","36","2"

Looks like the output of the query is already a valid set of CSV rows so you can probably just write it as is to a file.

# Your code

import requests

params = {
  "api_key": "tSKqprcg7k-S",
  "format": "csv"
  }
r = requests.get('https://www.parsehub.com/api/v2/projects/tq1H9ha1sKAQ/last_ready_run/data', params=params)
print(r.text)

# This bit of code will write the result of the query to output.csv

with open('output.csv', 'w+') as f:
    f.write(r.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM