Python 怎么将列表类字典组字符串转换为列表? 20
[{"id":100220748688221696701923815,"ts":1614576522050,"tradeId":7500734,"amount":1152.43,"price":0.01852,"direction":"sell"}]
怎么将他转换成字典类列表,或者其他方法,我需要直接读取ID TS tradeID 项目
注意 最后一项direction 项目不像其他的没有冒号 展开
可能有一些没说清楚,字符串是通过
compressData = ws.recv()
result = gzip.decompress(compressData).decode('utf-8')
解析出来,通过查找截取字符后生成
[{"id":100220748688221696701923815,"ts":1614576522050,"tradeId":7500734,"amount":1152.43,"price":0.01852,"direction":"sell"}] 列表样的字符串
我用了json.loads 和EVAL 但是都不能变成list,
不知理解得对不对,你看看:
test = [
[{'id':11,
'ts':12,
'tradeId':13,
'amount':14,
'price':15,
'direction':'1'
}],
[{'id':21,
'ts':22,
'tradeId':23,
'amount':24,
'price':25,
'direction':'2'
}],
[{'id':31,
'ts':32,
'tradeId':33,
'amount':34,
'price':35,
'direction':'3'
}],
]
result = {'id':[],'ts':[],'tradeId':[],'amount':[],'price':[],'direction':[]}
for row in test:
for sub_row in row:
result['id'].append(sub_row['id'])
result['ts'].append(sub_row['ts'])
result['tradeId'].append(sub_row['tradeId'])
result['amount'].append(sub_row['amount'])
result['price'].append(sub_row['price'])
result['direction'].append(sub_row['direction'])
由于太复杂的功能不会用列表推导式,所以就做成循环了,即最后是一个字典,字典中的值是列表: