やりたいこと
下記のjsonのデータからcreatedAt・title・messageIdの項目を取得しております。
jsonファイルから取得した各項目をエクセルへ貼り付けたいです。
Jsonファイル
{ "tokens": [ "アメリカ" ], "page": 1, "totalCount": 3, "recordCount": 3, "hasMore": false, "isLimited": false, "records": [ { "roomId": 18913457, "messageId": 2276890177, "writerId": 13489639, "contentType": "post", "text": "アメリカ\n1", "createdAt": "2022-05-20T03:53:39.654Z", "updatedAt": "2022-05-20T03:53:39.668Z", "commentCount": 0, "pollId": -1, "postId": -1, "todoId": -1, "from": "web", "isThreaded": false, "mentions": [], "post": { "id": 2276890177, "title": "アメリカ", "body": "1" }, "target": { "from": 2276890177, "id": 2276890177, "contentType": "post", "title": "アメリカ\n1", "roomId": 18913457, "contentTypeId": 2276890177, "linkId": 2270092589 } }, { "roomId": 18913457, "messageId": 2276891330, "writerId": 13489639, "contentType": "post", "text": "アメリカ>ロス\n1", "createdAt": "2022-05-20T03:54:20.650Z", "updatedAt": "2022-05-20T03:54:20.671Z", "commentCount": 0, "pollId": -1, "postId": -1, "todoId": -1, "from": "web", "isThreaded": false, "mentions": [], "post": { "id": 2276891330, "title": "アメリカ>ロス", "body": "1" }, "target": { "from": 2276891330, "id": 2276891330, "contentType": "post", "title": "アメリカ>ロス\n1", "roomId": 18913457, "contentTypeId": 2276891330, "linkId": 2270093714 } }, { "roomId": 18913457, "messageId": 2276891991, "writerId": 13489639, "contentType": "post", "text": "アメリカ>ニューヨーク\n2", "createdAt": "2022-05-20T03:54:40.624Z", "updatedAt": "2022-05-20T03:54:40.657Z", "commentCount": 0, "pollId": -1, "postId": -1, "todoId": -1, "from": "web", "isThreaded": false, "mentions": [], "post": { "id": 2276891991, "title": "アメ\nリカ>ニューヨーク", "body": "2" }, "target": { "from": 2276891991, "id": 2276891991, "contentType": "post", "title": "アメリカ>ニューヨーク\n2", "roomId": 18913457, "contentTypeId": 2276891991, "linkId": 2270094355 } } ] }
実現したエクセルの処理結果
各カラムごとにデータを出力をしたいです。
createdAt | title | messageId |
---|---|---|
2022-05-20T03:53:39.654Z | アメリカ | 2276890177 |
2022-05-20T03:54:20.650Z | アメリカ>ロス | 2276891330 |
2022-05-20T03:54:40.624Z | アメリカ>ニューヨーク | 2276891991 |
現在の結果
全てのリストが1行のみに書き出しされます。
各カラム・行ごとにデータを出力するにはどのようにdataを変更すれば良いでしょうか。
もし分かる方がいましたら、教えていただけると幸いです。
createdAt | title | messageId |
---|---|---|
['2022-05-20T03:53:39.654Z', '2022-05-20T03:54:20.650Z', '2022-05-20T03:54:40.624Z'] | ['アメリカ', 'アメリカ>ロス', 'アメ\nリカ>ニューヨーク'] | [2276890177, 2276891330, 2276891991] |
全体のコード
import json import pandas as pd #jsonファイル読み込み json_open = open("C:\\Users\\test\\Desktop\\test.json", 'r') json_load = json.load(json_open) print(json_load) #エクセルファイル excelfile="C:\\Users\\test\\Desktop\\test.xlsx" #作成日時取得 createdAt= [x["createdAt"] for x in json_load["records"]] print(createdAt) #['2022-05-20T03:53:39.654Z', '2022-05-20T03:54:20.650Z', '2022-05-20T03:54:40.624Z'] #タイトル取得 title= [x["post"]["title"] for x in json_load["records"]] print(title) #['アメリカ', 'アメリカ>ロス', 'アメリカ>ニューヨーク'] #メッセージID取得 messageId= [x["messageId"] for x in json_load["records"]] print(messageId) #[2276890177, 2276891330, 2276891991] #Header作成 col = ["createdAt","title","messageId"] #Headerのみdataframe作成 df = pd.DataFrame(columns=col) #dataframe作成 data = {'createdAt' : createdAt, 'title' : title, 'messageId' : messageId} #新しい行に値を追加 df_new = df.append(data, ignore_index=True) #xlsx書き出し df_new.to_excel(excelfile, sheet_name="sheet", index=False, header=True)
お手数ですが、よろしくお願い致します。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/23 04:04
2022/05/23 04:25