tags:

views:

99

answers:

3

I have a string that looks something like this:

[["Name1","ID1","DDY1", "CALL1", "WHEN1"], ["Name2","ID2","DDY2", "CALL2", "WHEN2"],...];

This string was taken from a website. There can be any amount of groupings. How could I parse this string and print just the Name variables of each grouping?

A: 
data = '''[["Name1","ID1","DDY1", "CALL1", "WHEN1"], ["Name2","ID2","DDY2", "CALL2", "WHEN2"]]'''
data = eval(data) #this is either eval or raw_input, I'm not sure
for x in data:
    print x[0]

This is something off the top of my head, I'm at work and don't have python installed so I can't test it out for you.

Jeroen Pelgrims
Tried it. It prints every character on it's own line
j00niner
That won't work. It will print each character in the string data on its own line.
wump
Yea, i mistaked list() with eval() or raw_input(), corrected my mistake.
Jeroen Pelgrims
This would probably work, but eval is *dangerous*. If you don't validate the string well, the site could execute any python code in your context.
wump
+6  A: 

Hope I understood well.

>>> import json
>>> a = json.loads('[["Name1","ID1","DDY1", "CALL1", "WHEN1"], ["Name2","ID2","DDY2", "CALL2", "WHEN2"]]')
>>> [x[0] for x in a]
[u'Name1', u'Name2']
>>> 
Marco Mariani
+1 for simply using the JSON parser
wump
"No JSON object could be decoded"
j00niner
Then you need to show the full string. It could be a mangled json or something else entirely.
Marco Mariani
You may need to strip a trailing comma after the last grouping. Throw the string into jslint - it will tell you what's wrong. Then massage the string so it works and do the above. You should *really* avoid using eval on a string taken from another website.
adamnfish
Also, the string you are passing to json.loads should be a unicode object. Check that, or decode appropriately from utf-8 or whatever encoding.
Marco Mariani
Problem at line 1 character 194: Expected '(end)' and instead saw ';'.
j00niner
strip the ";" off the end.
Gabriel
Do I put [u'Name1', u'Name2'] in my IDE also or is that only for shell?
j00niner
I mean [x[0] for x in a]
j00niner
oh, it was just to show the output, if the 'Name' strings are what you are after.
Marco Mariani
+2  A: 
import ast
y = ast.literal_eval(input)
[x[0] for x in y]

Thanks to @stephan for pointing me in the right direction with ast.literal_eval. As described by the documentaion:

Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.

Note: this is new functionality in Python 2.6.

Gabriel
Suggest `ast.literal_eval()` instead of `eval`.
stephan
Fixed, thanks. I knew it was around and I was searching for it. http://docs.python.org/library/ast.html ftw.
Gabriel
You are welcome. Another tiny improvement: `str` is a built-in class, hence I wouldn't use this name in the list comprehension.
stephan
fixed. Thanks again stephan.
Gabriel