parsing CSV files with quotes
Warren Postma
embed at geocities.com
Thu Mar 30 12:12:02 EST 2000
Okay, here's a hackish way to do it:
>>> x = "34, \"Stone Cold, Austin,Steve \", 50"
>>> print x
34, "Stone Cold, Austin,Steve ", 50
>>> y = eval("["+x+"]")
>>> print y
[34, 'Stone Cold, Austin,Steve ', 50]
>>> print str(y)[1:-1]
34, 'Stone Cold, Austin,Steve ', 50
Well, one thing, for the picky is that Python prefers single quotes, when
you do a str(), it uses that.
Another is that any code with eval() can do "way more than I intended" by
calling eval, since eval() is basically opening up the entire parser.
Is there some code to do eval_literals_only() that knows about parsing
literal expressions but which can't be made to call any functions?
ie:
l = eval_literals_only("["+x+"]") # create list, but would not allow
function calls
Weird to realize you're on the path to reinventing some tiny, yet
nontrivial, portion of Python's parser from within Python, eh?
Warren
More information about the Python-list
mailing list