import json
import requests
product_code = input("Enter the product code: ")
r = requests.get(f'https://api.fda.gov/device/510k.json?search=product_code:{product_code}')
packages_json = r.json()
packages = json.dumps(packages_json["results"], indent=2)
#display desired product code info
print(packages)
# https://api.fda.gov/device/510k.json?search=product_code:QBJ
我的代码目前只是打印出 JSON 文件。我希望我能得到一些帮助或指导,以视觉上令人愉悦的格式显示数据。我认为 CSV 类似于这个 site 。但任何其他想法将不胜感激。谢谢
答案 0 :(得分:0)
此问题已有答案here。
虽然有多种方法可以将 JSON 转换为 CSV 文件,但使用 pandas 库会容易得多。
以下命令将为您的问题提供解决方案:
import pandas as pd
df = pd.read_json()
df.to_csv("Your File")
答案 1 :(得分:0)
你可以使用 PANDAS 库:
import pandas as pd
df = pd.read_json (r'Path where the JSON file is saved\File Name.json')
df.to_csv (r'Path where the new CSV file will be stored\New File Name.csv', index = None)
您可以使用 TKINTER 包来直观地显示它:
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()
label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)
def getJSON ():
global read_file
import_file_path = filedialog.askopenfilename()
read_file = pd.read_json (import_file_path)
browseButton_JSON = tk.Button(text=" Import JSON File ", command=getJSON, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browseButton_JSON)
def convertToCSV ():
global read_file
export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
read_file.to_csv (export_file_path, index = None, header=True)
saveAsButton_CSV = tk.Button(text='Convert JSON to CSV', command=convertToCSV, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveAsButton_CSV)
def exitApplication():
MsgBox = tk.messagebox.askquestion ('Exit Application','Are you sure you want to exit the application',icon = 'warning')
if MsgBox == 'yes':
root.destroy()
exitButton = tk.Button (root, text=' Exit Application ',command=exitApplication, bg='brown', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 230, window=exitButton)
root.mainloop()
此外,您可以检查 here