如何从python中的数据创建.csv或.xlsx文件


[
{
"name":"Iphone XR",
"price" :"$500",
"Reviews":{"user":{"Name":"mr.Y","id":122145,"review":'Wow'},
"user2":{"Name":"mr.Z","id":545145,"review":'Wow 1'},
"user3":{"Name":"mr.A","id":541274,"review":'Wow 2'},}
},
{
"name":"Iphone 13",
"price" :"$1500",
"Reviews":{"user1":{"Name":"mr.S","id":5435632,"review":'Wow11'},
"user2":{"Name":"mr.F","id":546645,"review":'Wow22'},
"user3":{"Name":"mr.G","id":255863,"review":'Wow33'},}
},
]

我在用python。我有这些数据。如何获得漂亮的excel或任何类似excel的输出。

我不太擅长节目。像这样的输出:

No.  P-name  price  Reviews  Name  id  review     Name  id  review
1    Ipho..  $500            Name  4.. Good Phone Mr.. .... ......
2 
................................................

我正在使用熊猫,但没有得到相同的结果。对此数据的任何建议或解决方案。

这个代码应该完成以下工作:

import pandas as pd
import json
json1 = """
[
{
"name":"Iphone XR",
"price" :"$500",
"Reviews":{"user":{"Name":"mr.Y","id":122145,"review":'Wow'},
"user2":{"Name":"mr.Z","id":545145,"review":'Wow 1'},
"user3":{"Name":"mr.A","id":541274,"review":'Wow 2'}}
},
{
"name":"Iphone 13",
"price" :"$1500",
"Reviews":{"user1":{"Name":"mr.S","id":5435632,"review":'Wow11'},
"user2":{"Name":"mr.F","id":546645,"review":'Wow22'},
"user3":{"Name":"mr.G","id":255863,"review":'Wow33'}}
}
]
"""

json1 = json1.replace("'", '"')
response = json.loads(json1)
pd.DataFrame.from_dict(response)
reviews = ''
res = pd.DataFrame()
for _, review in pd.DataFrame.from_dict(response).iterrows():
item, price, reviews = review
items = reviews.items()
aux1 = pd.DataFrame( [ i for i, j in items ], columns=['User']  )
.merge(pd.DataFrame( [ j for i, j in items ] ), left_index=True, right_index=True, how='inner')
index = [(i, item, price) for i in range(len(aux1))]
aux1.index = pd.Index(index)
if res.empty:
res = aux1.copy()
else:
res = res.append(aux1, ignore_index=False, sort=False)
res.to_excel(r'path/to/ouput/your/excel.xlsx')

相关内容

  • 没有找到相关文章

最新更新