将输出从 API 转换为 CSV
Converting output from API to CSV
我正在尝试为一个名为 "ParseHub" 的平台使用 API。他们有在 Python 中使用它的示例代码,但不幸的是我在 Python 中不是最好的,我无法弄清楚如何将文件保存为 CSV...这是代码:
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)
如何将输出保存为 CSV 或 excel 文件?
谢谢!
编辑:这是输出的样子。这只是一大堆这样的行
"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"
看起来查询的输出已经是一组有效的 CSV 行,因此您可以直接将其写入文件。
# 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)
我正在尝试为一个名为 "ParseHub" 的平台使用 API。他们有在 Python 中使用它的示例代码,但不幸的是我在 Python 中不是最好的,我无法弄清楚如何将文件保存为 CSV...这是代码:
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)
如何将输出保存为 CSV 或 excel 文件?
谢谢!
编辑:这是输出的样子。这只是一大堆这样的行
"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"
看起来查询的输出已经是一组有效的 CSV 行,因此您可以直接将其写入文件。
# 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)