我看了很多youtube视频,并看了几个论坛,但是,我找不到问题的答案,因为也许我的数据结构很奇怪,而且我是python新手,所以这对我来说很复杂。
我有一个看起来像这样的“ data.json”文件:
{
"570": {
"appid": 570,
"name": "Dota 2",
"developer": "Valve",
"publisher": "Valve",
"score_rank": 65,
"positive": 848043,
"negative": 136246,
"userscore": 86,
"owners": "100,000,000 .. 200,000,000",
"average_forever": 29576,
"average_2weeks": 1541,
"median_forever": 523,
"median_2weeks": 823,
"price": "0",
"initialprice": "0",
"discount": "0"
},
"578080": {
"appid": 578080,
"name": "PLAYERUNKNOWN'S BATTLEGROUNDS",
"developer": "PUBG Corporation",
"publisher": "PUBG Corporation",
"score_rank": 10,
"positive": 481531,
"negative": 468448,
"userscore": 49,
"owners": "50,000,000 .. 100,000,000",
"average_forever": 19962,
"average_2weeks": 762,
"median_forever": 10055,
"median_2weeks": 271,
"price": "2999",
"initialprice": "2999",
"discount": "0"
}
}
1。
import json, csv
infile = open("C:\Users\....\data.json", "r")
outfile = open("C:\Users\....\bar.csv", "w")
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.write(row)
import sys
import pandas as pd
from pandas import DataFrame
import json
data=r'C:\Users\....\data.json'
print ("This is json data input", data)
/* Reads and converts json to dict.
def js_r(data):
with open(data, encoding='utf-8') as f_in:
return(json.load(f_in))
if __name__ == "__main__":
my_dic_data = js_r(data)
print("This is my dictionary", my_dic_data)
keys= my_dic_data.keys()
print ("The original dict keys",keys)
/* You assign a new dictionary key- SO_users, and make dictionary comprehension = { your_key: old_dict[your_key] for `enter code here`your_key in your_keys }
dict_you_want={'my_items':my_dic_data[0]for key in keys}
print ("These are the keys to dict_you_want",dict_you_want.keys())
print ("This is the dictionary of SO_users", dict_you_want)
df=pd.DataFrame(dict_you_want)
print ("df:", df)
/*When .apply(pd.Series) method on items column is applied, the dictionaries in items column will be used as column headings
df2=df['my_items'].apply(pd.Series)
print ("df2",df2)
df3=pd.concat([df2.drop(['user'],axis=1),df2['user'].apply(pd.Series)],axis=1)
/*df3=df2['user'].apply(pd.Series)
print ("df3",df3)
以上任何链接均无法产生错误。 我想做的是我想要的:
打开data.json
从json读取以下列,并将其放入data.csv(与data.json相同的位置): “ appid”,“ name”,“ developer”,“ publisher”,“ score_rank”,“ positive”,“ negative”,“ userscore”,“ owners”,“ average_forever”,“ average_2weeks”,“ median_forever”,“ median_2weeks” ”,“价格”,“初始价格”,“折扣”
不包括第一列,该列基本上是每一行的唯一标识符,并且所有上述子属性在每个唯一标识中都重复。
您能指导我完成这个吗?
答案 0 :(得分:0)
您可以这样做:
import json, csv
data = json.load(open('data.json', 'r'))
with open('data.csv', 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = [...] # whatever fields in whatever order you want
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for appid, appinfo in data.items():
writer.writerow(appinfo)