one-element tuples

Fillmore fillmore_remove at hotmail.com
Sun Apr 10 23:19:59 EDT 2016


Thank you for trying to help, Martin. So:

On 04/10/2016 09:08 PM, Martin A. Brown wrote:
> #1: I would not choose eval() except when there is no other
>      solution.  If you don't need eval(), it may save you some
>      headache in the future, as well, to find an alternate way.
>      So, can we help you choose something other than eval()?
>      What are you trying to do with that usage?

so, I do not quite control the format of the file I am trying to parse.

it has the format:

"str1","str2",....,"strN" => more stuff
   :

in some cases there is just one "str" which is what created me problem.
The first "str1" has special meaning and, at times, it can be alone.

The way I handle this is:

     parts = line.strip().split(" => ")
     tokens = eval(parts[0])

     if type(tokens) == str:   #Handle case that there's only one token
         columns.add(tokens)
         rowTokenString = "__Empty__"
         rows.add(rowTokenString)
         value = parts[1][:2]
         addCell(table, rowTokenString, tokens, value)
     else:
         columns.add(tokens[0])
         rowTokenString = '"'+'","'.join(tokens[1:]) + '"'
         rows.add(rowTokenString)
         value = parts[1][:2]
         addCell(table, rowTokenString, tokens[0],value)

which admittedly is not very elegant. If you have suggestions on how to avoid the use
of eval() and still achieve the same, I would be delighted to hear them





More information about the Python-list mailing list