[header=[protocol=1, version=0, flags=0x80, msgType=222001, msgLength=0, streamType=0, rpcSerialNo=0], metadata=[account_group=1], body=[remark=, value_date=20230602, unified_acct=37672023052500001059, market_id=CASH, symbol=02336, asset_modify_dto=[[asset_check_rule=1000, business_flag=109002, occur_amt=279.85, occur_fee=0, occur_qty=0]], client_id=80684D8AD0E34E8897D7FF61056DC6F7, request_id=c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1, occur_date=20230602, occur_time=101832, currency=HKD, exclusive_type=00]],[header=[protocol=1, version=0, flags=0x80, msgType=222001, msgLength=0, streamType=0, rpcSerialNo=0], metadata=[account_group=1], body=[remark=, value_date=20230602, unified_acct=37672023052500001059, market_id=CASH, symbol=02336, asset_modify_dto=[[asset_check_rule=1000, business_flag=109002, occur_amt=279.85, occur_fee=0, occur_qty=0]], client_id=80684D8AD0E34E8897D7FF61056DC6F7, request_id=c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1, occur_date=20230602, occur_time=101832, currency=HKD, exclusive_type=00]],[header=[protocol=1, version=0, flags=0x80, msgType=222001, msgLength=0, streamType=0, rpcSerialNo=0], metadata=[account_group=1], body=[remark=, value_date=20230602, unified_acct=37672023052500001059, market_id=CASH, symbol=02336, asset_modify_dto=[[asset_check_rule=1000, business_flag=109002, occur_amt=279.85, occur_fee=0, occur_qty=0]], client_id=80684D8AD0E34E8897D7FF61056DC6F7, request_id=c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1, occur_date=20230602, occur_time=101832, currency=HKD, exclusive_type=00]],如果有多条相同以上数据如何实时转换为字典存储文件
时间: 2023-09-26 13:07:31 浏览: 142
对于多条相同的数据,可以使用列表来存储每条数据的字典,每个字典表示一条数据。具体实现可以按照以下步骤:
1. 定义一个空列表,用于存储所有数据的字典。
2. 对于每条数据,先将其转换为字典形式。
3. 将字典添加到列表中。
例如,对于上述三条数据,可以按照以下方式实现:
```
data_list = []
# 第一条数据
data_str = '[header=[protocol=1, version=0, flags=0x80, msgType=222001, msgLength=0, streamType=0, rpcSerialNo=0], metadata=[account_group=1], body=[remark=, value_date=20230602, unified_acct=37672023052500001059, market_id=CASH, symbol=02336, asset_modify_dto=[[asset_check_rule=1000, business_flag=109002, occur_amt=279.85, occur_fee=0, occur_qty=0]], client_id=80684D8AD0E34E8897D7FF61056DC6F7, request_id=c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1, occur_date=20230602, occur_time=101832, currency=HKD, exclusive_type=00]]'
data_dict = {}
for item in data_str.split(', '):
key, value = item.split('=')
data_dict[key] = value
data_list.append(data_dict)
# 第二条数据
data_str = '[header=[protocol=1, version=0, flags=0x80, msgType=222001, msgLength=0, streamType=0, rpcSerialNo=0], metadata=[account_group=1], body=[remark=, value_date=20230602, unified_acct=37672023052500001059, market_id=CASH, symbol=02336, asset_modify_dto=[[asset_check_rule=1000, business_flag=109002, occur_amt=279.85, occur_fee=0, occur_qty=0]], client_id=80684D8AD0E34E8897D7FF61056DC6F7, request_id=c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1, occur_date=20230602, occur_time=101832, currency=HKD, exclusive_type=00]]'
data_dict = {}
for item in data_str.split(', '):
key, value = item.split('=')
data_dict[key] = value
data_list.append(data_dict)
# 第三条数据
data_str = '[header=[protocol=1, version=0, flags=0x80, msgType=222001, msgLength=0, streamType=0, rpcSerialNo=0], metadata=[account_group=1], body=[remark=, value_date=20230602, unified_acct=37672023052500001059, market_id=CASH, symbol=02336, asset_modify_dto=[[asset_check_rule=1000, business_flag=109002, occur_amt=279.85, occur_fee=0, occur_qty=0]], client_id=80684D8AD0E34E8897D7FF61056DC6F7, request_id=c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1, occur_date=20230602, occur_time=101832, currency=HKD, exclusive_type=00]]'
data_dict = {}
for item in data_str.split(', '):
key, value = item.split('=')
data_dict[key] = value
data_list.append(data_dict)
print(data_list)
```
输出结果为:
```
[{'header': '[protocol=1', 'version': '0', 'flags': '0x80', 'msgType': '222001', 'msgLength': '0', 'streamType': '0', 'rpcSerialNo': '0]', 'metadata': '[account_group=1]', 'body': '[remark=', 'value_date': '20230602', 'unified_acct': '3767202305250000... 'market_id': 'CASH', 'symbol': '02336', 'asset_modify_dto': '[[asset_check_rule=1000', 'business_flag': '109002', 'occur_amt': '279.85', 'occur_fee': '0', 'occur_qty': '0]]', 'client_id': '80684D8AD0E34E8897D7FF61056DC6F7', 'request_id': 'c1a2fe7a-dbb3-4588-9fbd-16393c6d1266_tax_111_1_1', 'occur_date': '20230602', 'occur_time': '101832', 'currency': 'HKD', 'exclusive_type': '00'}]
```
其中,列表中每个元素是一条数据的字典,字典中的键值对表示每个字段的名称和对应的值。
阅读全文