我试图将以下JSON文件转换为csv文件。
JSON文件
[{
"SubmitID":1, "Worksheet":3, "UserID":65,
"Q1":"395",
"Q2":"2178",
"Q3":"2699",
"Q4":"1494"},{
"SubmitID":2, "Worksheet":3, "UserID":65,
"Q4":"1394"},{
"SubmitID":3, "Worksheet":4, "UserID":65,
"Q1":"1629",
"Q2":"1950",
"Q3":"0117",
"Q4":"1816",
"Empty":" "}]
但是,我下面的Python代码给出了错误消息“TypeError:Expected String or Unicode”。我是否应该知道如何修改我的程序才能使其正常工作?
import json
import pandas as pd
f2 = open('temp.json')
useful_input = json.load(f2)
df=pd.read_json(useful_input)
print(df)
df.to_csv('results.csv')
答案 0 :(得分:4)
您只需将地址字符串传递给pd.read_json()
:
df=pd.read_json("temp.json")
答案 1 :(得分:3)
您不必使用json模块:
尝试:
import pandas as pd
df=pd.read_json("temp.json")
print(df)
df.to_csv('results.csv')
答案 2 :(得分:1)
import pandas as pd
df = pd.read_json('data.json')
df.to_csv('data.csv', index=False, columns=['title', 'subtitle', 'date', 'description'])
import pandas as pd
df = pd.read_csv("data.csv")
df = df[df.columns[:4]]
df.dropna(how='all')
df.to_json('data.json', orient='records')