2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
json_normalize將 pandas 中的記錄展平/結(jié)構(gòu)化
為了美化,將Hotel.name移到第一列
使用 pandas 功能輸出為 CSV
import pandas as pd
js = {
"Hotel": [
{
"name": "Concorde Hotel New York",
"Reviewer": [
{
"name": "Serje J",
"Title": "Didn't make it but hope to soon",
"Title_url": "https://www.tripadvisor.com/ShowUserReviews-g60763-d14040955-r751642278-Concorde_Hotel_New_York-New_York_City_New_York.html",
"Description": "I was scheduled to stay here in March which unfortunately didn't happen due to COVID-19. I was forced to cancel my arrangements, however I was so impressed by the courtesy and promptitude with which Concorde staff arranged my cancellation and refund. When all this madness is behind us I will certainly be rebooking with this hotel.",
"Location": "Melbourne, Australia",
"Date": "Date of stay: March 2020",
"Rating": "ui_bubble_rating bubble_50"
},
{
"name": "John Schueler",
"Title": "Beautiful Modern Rooms in East MidTown",
"Title_url": "https://www.tripadvisor.com/ShowUserReviews-g60763-d14040955-r751405066-Concorde_Hotel_New_York-New_York_City_New_York.html",
"Description": "The lobby is under construction but the rooms are fresh, modern and very nicely appointed. The bathrooms are large and very nice. Only complaint was the wifi was weak in the room - which could be due to the construction disruption. Great location for sites and restaurants. Room was very quiet!",
"Location": "Beaufort, South Carolina",
"Date": "Date of stay: March 2020",
"Rating": "ui_bubble_rating bubble_40"
}
]
}
]
}
# use provided functionality to flatten and normalise data
df = pd.json_normalize(js, record_path=["Hotel","Reviewer"], meta=[["Hotel","name"]])
# Hotel.name logically should be 1st column, move it
df.insert(0, "Hotel.name", df.pop("Hotel.name"))
# make it a CSV....
df.to_csv(index=False)

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
首先,我建議您始終將 Json 粘貼到查看器中。這樣您就可以輕松地可視化文件結(jié)構(gòu)。
然后,將您提供的示例保存為file.json,只需幾行代碼即可獲得:
import pandas as pd
file = pd.read_json("file.json")
temp = file['Hotel'][0]['Reviewer']
df = pd.DataFrame.from_dict(temp)
df['hotel'] = file['Hotel'][0]['name']
>> df
name ... hotel
0 Serje J ... Concorde Hotel New York
1 John Schueler ... Concorde Hotel New York
添加回答
舉報(bào)