python unicode列表,重复双重双引号

时间:2018-04-19 21:46:11

标签: python list unicode

我正在通过龙卷风websocket进行一些提取,并像这样回收json数据:

msg = [157,"tu","27213579-SD",229156181,1524173145,8265,0.08]

type(msg) - >的unicode

现在我必须将其转换为数据类型列表。 我的第一个意图如下:

msg = [e.encode('utf-8') for e in msg.strip('[]').split(',')]

但现在有双引号

msg =["157",""tu"",""27213579-SD"","229156181","1524173145","8265","0.08"]

你知道一个聪明的方法来获得一个干净的python列表吗?

1 个答案:

答案 0 :(得分:0)

使用json模块将JSON转换为数据:

#!python2
import json

msg = u'[157,"tu","27213579-SD",229156181,1524173145,8265,0.08]'
print type(msg)
data = json.loads(msg)
print type(data)
print data

输出:

<type 'unicode'>
<type 'list'>
[157, u'tu', u'27213579-SD', 229156181, 1524173145, 8265, 0.08]