追記したい大本のJSON
JSON
1{ 2 "name": "test1", 3 "responses": [ 4 { 5 "action": "test1", 6 "parameters": 7 [ 8 {"name": "関数1","dataType": "@関数","value": "$関数1"}, 追加したい項目1 9 {"name": "関数2","dataType": "@関数","value": "$関数2"} 10 11 ], 12 "messages": 13 [ 14 {"type": "message","condition": "","speech": ["$関数1説明"]} , 追加したい項目2 15 {"type": "message","condition": "","speech": ["$関数2説明"]} 16 ], 17 "resetContexts": "false" 18 } 19 ], 20 "userSays": [ 21 { 22 "isTemplate": "false", 23 "data": 24 [ 25 {"text": "内容","alias": "関数1","meta": "@関数"},{"text": "内容"}, 追加したい項目3 26 {"text": "内容","alias": "関数2","meta": "@関数"},{"text": "内容"} 27 ], 28 "count": 0, 29 "updated": "null" 30 } 31 ], 32 "webhookForSlotFilling": "false", 33 "webhookUsed": "false" 34}
コード
python
1import pandas as pd 2data = pd.read_csv('/content/test.csv').values.tolist() 3 4name=[] 5message=[] 6alias=[] 7for i in range(len(data)): 8 name.append('{"name": "関数'+str(i)+'","dataType": "@関数","value": "$関数'+str(i)+'"}'); 9 message.append('{"type": "message","condition": '',"speech": ["$関数'+str(i)+'は'+input(f"{data[i]}説明を入力")+'です"]}'); 10 alias.append('{"text": "'+input(f"{data[i]}の内容を入力")+'","alias": "関数'+str(i)+'","meta": "@関数"},{"text": "内容"}'); 11 12 13name_json= ','.join(name) 14message_json= ','.join(message) 15alias_json= ','.join(alias) 16 17json_data={"name": "test1","responses": [{"action": "test1","parameters": [name_json],"messages": [message_json],"resetContexts": "false"}],"userSays": [{"isTemplate": "false","data": [alias_json],"count": 0,"updated": "null"}],"webhookForSlotFilling": "false","webhookUsed": "false"} 18 19import json 20with open('mydata.json', mode='wt', encoding='utf-8') as file: 21 json.dump(json_data, file, ensure_ascii=False, indent=2)
名前の入ったcsvファイルを読み込んで
追加したい要素ごとに配列を作って
appendで追加をしたあと
各配列の各要素の間にカンマを入れた文字列にして
jsonの大枠にいれて
jsonファイルで書き出し
そんなかんじのことをしているんですが
書き出されたファイルが
JSON
1{ 2 "name": "test1", 3 "responses": [ 4 { 5 "action": "test1", 6 "parameters": [ 7 "{\"name\": \"関数0\",\"dataType\": \"@関数\",\"value\": \"$関数0\"},{\"name\": 略 alue\": \"$関数14\"}" 8 ], 9 "messages": [ 10 "{\"type\": \"message\",\"condition\": ,\"speech\": [\"$関数0は〇〇です\"]},{\"type\": 略 ech\": [\"$関数14は〇〇です\"]}" 11 ], 12 "resetContexts": "false" 13 } 14 ], 15 "userSays": [ 16 { 17 "isTemplate": "false", 18 "data": [ 19 "{\"text\": \"内容前\",\"alias\": \"関数0\",\"meta\": \"@関数\"},{\"text\": \"内容後ろ\"},{\"text\": \ 略 e\"},{\"text\": \"内容後ろ\"}" 20 ], 21 "count": 0, 22 "updated": "null" 23 } 24 ], 25 "webhookForSlotFilling": "false", 26 "webhookUsed": "false" 27}
およそ思ったような形にはなったのですが ”が正規表現化?してしまって使える形になっていなくてpythonでjsonの追加をしたちと起動したら良いのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/04 06:29