简体   繁体   English

如何使用python将Web API的整个JSON响应转换为CSV文件?

[英]How to convert entire JSON response of Web API to CSV file using python?

I would like to convert entire string or "gudid" of such page to CSV file by python code. 我想通过python代码将这样的页面的整个字符串或“gudid”转换为CSV文件。

The results of the following code contains only part of the content (39 lines) of the above URL (125 lines after formatted by ms-python.python). 以下代码的结果仅包含上述URL的部分内容(39行)(由ms-python.python格式化后的125行)。

import requests
import json
import pandas as pd
RESORCE_INFORMATION = "https://accessgudid.nlm.nih.gov/api/v2/devices/lookup.json"

PARAMTERS = "di=08717648200274" 

TARGET = RESORCE_INFORMATION+'?'+PARAMTERS
print(TARGET)
response = requests.get(TARGET) 
# print(response.text)

json_dict = json.loads(response.text)
df = pd.DataFrame(json_dict["gudid"])
df.to_csv("device_lookup.csv")

How do I convert entire response from above URL to CSV file? 如何将上述URL的整个响应转换为CSV文件?

Thanks in advance, 提前致谢,

Below will work if it is not mandatory to use data frame. 如果不强制使用数据框,下面的内容将起作用。 also I see a filtering criteria, you converting only "gudud" column to df, is there any reason?...if thats not required remove that filtering 我也看到了一个过滤标准,你只将“gudud”列转换为df,有什么原因吗?...如果那不需要删除那个过滤

df = pd.DataFrame(json_dict["gudid"])

import csv
import json

json_dict = json.loads(response.text)
f = open('data.csv')
csv_file = csv.writer(f)
for item in json_dict:
    f.writerow(item)

f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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