0

I'm doing some fetching via tornado websocket and recive json data like this:

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

type(msg) -> unicode

now I have to convert it into datatype list. My first intention was the following:

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

but now there are double quotes

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

Do you know a smart way to get a clean python list?

1 Answers1

0

Use the json module to convert JSON to data:

#!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

Output:

<type 'unicode'>
<type 'list'>
[157, u'tu', u'27213579-SD', 229156181, 1524173145, 8265, 0.08]
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251