1

I've this string:

"{'osFreeMem': 286494720, 'osUpTime': 19230718, 'sysload': [0.24609375, 0.62109375, 0.734375], 'nodeUpTime': 1831, 'osTotMem': 536870912, 'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760}}"

I want to have a dictionary with these values:

'osFreeMem': 286494720
'osUpTime': 19230718
'sysload': [0.24609375, 0.62109375, 0.734375]    
'nodeUpTime': 1831
'osTotMem': 536870912    
'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760}

How i can get this list?

3
  • 1
    Any reason you dont want to use a JSON parser? Commented Mar 12, 2013 at 12:23
  • 1
    Where did you get that string from? You could probably get the data directly. If you can't get the data directly, I would switch the single quotes to double and parse it as JSON. Commented Mar 12, 2013 at 12:24
  • I get it from a server, by a websocket. How can i parse it with JSON PARSER? Commented Mar 12, 2013 at 12:26

2 Answers 2

5
In [37]: s = "{'osFreeMem': 286494720, 'osUpTime': 19230718, 'sysload': [0.24609375, 0.62109375, 0.734375], 'nodeUpTime': 1831, 'osTotMem': 536870912, 'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760}}"

In [38]: import ast

In [39]: d = ast.literal_eval(s)

In [40]: d
Out[40]: 
{'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760},
 'nodeUpTime': 1831,
 'osFreeMem': 286494720,
 'osTotMem': 536870912,
 'osUpTime': 19230718,
 'sysload': [0.24609375, 0.62109375, 0.734375]}

In [41]: d.items()
Out[41]: 
[('osFreeMem', 286494720),
 ('osUpTime', 19230718),
 ('sysload', [0.24609375, 0.62109375, 0.734375]),
 ('nodeUpTime', 1831),
 ('osTotMem', 536870912),
 ('nodeMemory', {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760})]

or

In [44]: import json

In [45]: json.loads(s.replace("'",'"')).items()
Out[45]: 
[(u'osFreeMem', 286494720),
 (u'osUpTime', 19230718),
 (u'sysload', [0.24609375, 0.62109375, 0.734375]),
 (u'nodeUpTime', 1831),
 (u'osTotMem', 536870912),
 (u'nodeMemory', {u'heapTotal': 8766304, u'heapUsed': 7789588, u'rss': 22773760})]
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for literal_eval - not convinced about replacing quotes and using json though
-1

You can also use the built in function eval

>>>d = eval("{'osFreeMem': 286494720, 'osUpTime': 19230718, 'sysload': [0.24609375, 0.62109375, 0.734375], 'nodeUpTime': 1831, 'osTotMem': 536870912, 'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760}}")
>>>d
{'osFreeMem': 286494720, 'osUpTime': 19230718, 'sysload': [0.24609375, 0.62109375, 0.734375], nodeUpTime': 1831, 'osTotMem': 536870912, 'nodeMemory': {'heapTotal': 8766304, 'heapUsed': 7789588, 'rss': 22773760}}

3 Comments

You can, but you should not, because eval is dangerous
It is dangerous only in case you allow users to hand input to it. Otherwise, it is as dangerous as any other command that might be used to execute commands (such as the commands module)
thanks. it works! i just had to do this: x = ast.literal_eval(message) result = map(lambda item: item[0] + ': ' + str(item[1]), x.items())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.