gpt4 book ai didi

python - 从 API 到 CSV 的数据框

转载 作者:行者123 更新时间:2023-12-03 16:59:03 26 4
gpt4 key购买 nike

我在 python 并尝试将数据从 API 导出到 csv

import requests
import pandas as pd


url = "https://api.bexio.com/2.0/article"
#r = requests.get('https://api.bexio.com/2.0/article')
headers = {
'Accept': "application/json",
'Content-Type': "application/json",
'Authorization': "Bearer xxx}
这很好用。
  [{"id":34,"user_id":1,"article_type_id":1,"contact_id":null,"deliverer_code":"","deliverer_name":"","deliverer_description":"","intern_code":"TEXLINE-SOLD","intern_name":"TEXLINE Sold out","intern_description":"","purchase_price":"0.900000","sale_price":"2.450000","purchase_total":null,"sale_total":null,"currency_id":1,"tax_income_id":16,"tax_id":16,"tax_expense_id":22,"unit_id":1,"is_stock":true,"stock_id":null,"stock_place_id":null,"stock_nr":0,"stock_min_nr":0,"stock_reserved_nr":0,"stock_available_nr":0,"stock_picked_nr":0,"stock_disposed_nr":0,"stock_ordered_nr":0,"width":null,"height":null,"weight":null,"volume":null,"html_text":null,"remarks":"
这里的问题是,我想将此数据导出到 csv,然后在表格中很好地列出。我试过
from pandas.io.json import json_normalize
dic = response.json()
df = pandas.DataFrame(dic)
df.to_csv('detections.csv', index=False, sep="\t")
或者用 Pandas 作为 pd。但这行不通。

最佳答案

试试 read_json如果您将 JSON 作为字符串:

df = pd.read_json(json_string)
或者,或者,使用 .json() 将 API 响应转换为 python 列表并直接创建一个 DataFrame从该列表中:
r = requests.get('https://jsonplaceholder.typicode.com/posts')
df = pd.DataFrame(r.json())

如果响应不是列表( [{..}, .., {..}] ),而是单个对象( {..} ):
r = requests.get('https://jsonplaceholder.typicode.com/posts/1')
df = pd.DataFrame(r.json(), index=[0])

关于python - 从 API 到 CSV 的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63722981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com