やりたいこと
下記のjsonのデータからbodyを取得します。
取得後、エクセルへ横方向で出力したいです。
Jsonファイル
{ "hasMore": false, "records": [ { "id": 11111778, "tmId": 11111111, "wrId": 22222222, "coType": "comment", "perm": 740, "upAt": "2022-12-19T06:11:40.632Z", "crAt": "2022-12-19T06:11:40.614Z", "isTh": false, "isEd": false, "attach": [ ], "menti": [ { "id": 11111111, "type": "member", "offset": 0, "length": 5 } ], "info": { "mentis": [ ] }, "liked": 0, "comm": 0, "formKey": null, "isForm": false, "tod": null, "postId": 2111111110, "pollId": null, "feeId": 2111111110, "shareEntities": [ 11111111 ], "status": "created", "linkPreviewId": null, "content": { "body": "テスト1", "connectInfo": [ ] }, "updateTime": 1571515700632, "createTime": 1571515700614, "deleterId": null, "from": "web", "isLiked": false, "isStarred": false }, { "id": 11111779, "tmId": 11111111, "wrId": 22222222, "coType": "comment", "perm": 740, "upAt": "2022-12-19T07:48:12.110Z", "crAt": "2022-12-19T07:48:12.089Z", "isTh": false, "isEd": false, "attach": [ ], "menti": [ { "id": 11111111, "type": "member", "offset": 0, "length": 6 } ], "info": { "mentis": [ ] }, "liked": 0, "comm": 0, "formKey": null, "isForm": false, "tod": null, "postId": 2111111110, "pollId": null, "feeId": 2111111110, "shareEntities": [ 11111111 ], "status": "created", "linkPreviewId": null, "content": { "body": "テスト2", "connectInfo": [ ] }, "updateTime": 1571522492110, "createTime": 1571522492089, "deleterId": null, "from": "web", "isLiked": false, "isStarred": false }, { "id": 2711111111, "tmId": 11111111, "wrId": 11111111, "coType": "comment", "perm": 740, "upAt": "2022-12-19T07:51:37.576Z", "crAt": "2022-12-19T07:49:13.758Z", "isTh": false, "isEd": false, "attach": [ ], "menti": [ { "id": 22222222, "type": "member", "offset": 0, "length": 6 } ], "info": { "mentis": [ ] }, "liked": 1, "comm": 0, "formKey": null, "isForm": false, "tod": null, "postId": 2111111110, "pollId": null, "feeId": 2111111110, "shareEntities": [ 11111111 ], "status": "created", "linkPreviewId": null, "content": { "body": "テスト3", "connectInfo": [ ] }, "updateTime": 1571522697576, "createTime": 1571522553758, "deleterId": null, "from": "web", "isLiked": false, "isStarred": false }, { "id": 2111111110, "tmId": 11111111, "wrId": 22222222, "coType": "comment", "perm": 740, "upAt": "2022-12-19T07:51:53.204Z", "crAt": "2022-12-19T07:51:53.178Z", "isTh": false, "isEd": false, "attach": [ ], "menti": [ { "id": 11111111, "type": "member", "offset": 0, "length": 6 } ], "info": { "mentis": [ ] }, "liked": 0, "comm": 0, "formKey": null, "isForm": false, "tod": null, "postId": 2111111110, "pollId": null, "feeId": 2111111110, "shareEntities": [ 11111111 ], "status": "created", "linkPreviewId": null, "content": { "body": "テスト4", "connectInfo": [ ] }, "updateTime": 1571522713204, "createTime": 1571522713177, "deleterId": null, "from": "web", "isLiked": false, "isStarred": false } ] }
現在結果
先ずはfor文で取り出したbodyを下記のように出力されます。
print(body)
テスト1 テスト2 テスト3 テスト4
エクセルの結果
列ごとに出力されます。
body1 | body2 | body3 | body4 |
---|---|---|---|
テスト1 | |||
テスト2 | |||
テスト3 | |||
テスト4 |
実現したい結果
横方向で出力するようにしたいです。
またbody毎回変化するので、自動でbody1,body2...のようにヘッダー書き込み可能でしょうか。
body1 | body2 | body3 | body4 |
---|---|---|---|
テスト1 | テスト2 | テスト3 | テスト4 |
解決方法やチェック事項などご存知ありましたらアドバイスいただきたく思います。
よろしくお願いします。
コード
import json import openpyxl json_open = open("C:\\Users\\test2\\Desktop\\test2.json", 'r') json_load = json.load(json_open) print(json_load) # エクセルを開く wb = openpyxl.Workbook() sheet = wb.active sheet .title = "Sheet1" ws = wb["Sheet1"] # シートの番号用変数 cnt = 1 #For文でbody取得 for key in json_load['records']: body=key['content']["body"] print(body) # シートの番号を決める sel1 = "A"+str(cnt) sheet[sel1].value = body cnt += 1 #1番目に行を追加 ws.insert_rows(1) #header sheet["A1"].value = "body1" sheet["B1"].value = "body2" sheet["C1"].value = "body3" sheet["D1"].value = "body4" # エクセルにデータを保存する wb.save("text.xlsx")

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/12/23 06:48