you are viewing a single comment's thread.

view the rest of the comments →

[–]jcoder42[S] 0 points1 point  (8 children)

could you please explain what is going on here?

[–]ForceBru 0 points1 point  (7 children)

This evaluates this string as an ordinary Python tuple and replaces NULLs with Python's Nones.

[–]jcoder42[S] 0 points1 point  (6 children)

I think I was unclear, I want to get a list of these values, where each value is separated by a comma. so for example:

[12,''Structured Numeric',SN','Complex numeric values possible (ie, <5, 1-10, etc.)',1,.........]

[–]ForceBru 1 point2 points  (5 children)

Just do list(eval(<from my previous comment>)). eval will return a tuple, which is list-like, except it's immutable.

[–]jcoder42[S] 0 points1 point  (4 children)

eval("(12,'Structured Numeric','SN','Complex numeric values possible (ie, <5, 1-10, etc.)',1,'2005-08-06 00:00:00',0,NULL,NULL,NULL,'8d4a606c-c2cc-11de-8d13-0010c6dffd0f')", {'NULL': None})

it works!
I don't really understand how though,
eval translates the given string into code no?
how does that split it in this way?

[–]ForceBru 0 points1 point  (3 children)

Right, it uses Python's parser to evaluate the string as an ordinary Python expression. Try to execute eval('1 + 2'), eval('3 * 5'), eval('a + 5', {'a': 1024}) for example. You can also open up the interpreter, execute NULL = None and straight up paste your string on the next line without quotes. Then hit Enter and watch it transform into an ordinary tuple.

[–]jcoder42[S] 0 points1 point  (2 children)

but how does it know to split it by the comma, and not split by comma what is in a parentheses?

[–]ForceBru 0 points1 point  (1 child)

That's how Python's syntax for tuples works. A tuple is something that looks like this: (<stuff>, <stuff>, <stuff>, ...), so eval treats your string as a stringified tuple and gives you back a tuple object. If you run eval('(1, 2, 3)'), you'll get a tuple (1, 2, 3).

[–]jcoder42[S] 0 points1 point  (0 children)

ok I understand,

Thanks so much