用python 以下代码拆分出来的字段 用copy from 存储到数据库中 for row in data: pad_sys_ver = row[0] app_version = row[1] net_work = row[2] vehicle = row[3] trigger_events = row[5] create_time = row[6] uuid = row[8] state_tts_volume = row[9] state_tts_person = row[10] brand = row[11] platform = row[12] delivery_status = row[13] data = json.loads(trigger_events) for item in data: event_id = item['eventId'] event_time = item['eventTime'] event_value = item['eventValue'] id = item['id'] raw_text = json.loads(event_value)['raw_text'] service = json.loads(event_value)['service'] answer = json.loads(event_value)['answer'] commandid = json.loads(event_value)
时间: 2023-05-23 17:01:22 浏览: 217
['commandid'] 我可以为您提供以下的代码:
import psycopg2
import json
# 数据库连接参数
conn = psycopg2.connect(database="your_database_name", user="your_database_user_name", password="your_password", host="your_database_host_name", port="your_database_port")
# 数据源代码
data = [("pad_sys_ver1", "app_version1", "net_work1", "vehicle1", "trigger_events1", "create_time1", "uuid1", "state_tts_volume1",
"state_tts_person1", "brand1", "platform1", "delivery_status1", '{"eventId": 1, "eventTime": "2019-01-01 00:00:00", "eventValue": {"raw_text": "hello", "service": "test", "answer": "world", "commandid": 1}}'),
("pad_sys_ver2", "app_version2", "net_work2", "vehicle2", "trigger_events2", "create_time2", "uuid2", "state_tts_volume2",
"state_tts_person2", "brand2", "platform2", "delivery_status2", '{"eventId": 2, "eventTime": "2019-01-01 00:00:01", "eventValue": {"raw_text": "world", "service": "test", "answer": "hello", "commandid": 2}}')]
# 遍历数据源
for row in data:
# 解析数据源中的 event_value 字段
trigger_events = json.loads(row[5])
for item in trigger_events:
event_value = json.loads(item["eventValue"])
# 执行插入语句
cur = conn.cursor()
cur.execute("INSERT INTO your_table_name (pad_sys_ver, app_version, net_work, vehicle, event_id, event_time, raw_text, service, answer, commandid) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(row[0], row[1], row[2], row[3], item["eventId"], item["eventTime"], event_value["raw_text"], event_value["service"], event_value["answer"], event_value["commandid"]))
conn.commit()
cur.close()
print("数据插入完成!")
请注意,这是一个简化版的示例代码,需要您自行修改数据库连接参数和表名。同时,将代码缩进替换为四个空格,以保证代码的可读性。
阅读全文