Boolean problem with string value from lIst

I have the following problem:

  # line is a line from a file that contains ["baa","beee","0"]
  line = TcsLine.split(",")
  NumPFCs = eval(line[2])
  if NumPFCs==0:
     print line

      

I want to print all lines from a file if the second position of the list has value == 0.

I print lines, but after that the following happens: Traceback (last call last):

['baaa', 'beee', '0', '\ n']

, but after the next ERROR

ilation.py", line 141, in ?
    getZeroPFcs()
ilation.py", line 110, in getZeroPFcs
    NumPFCs = eval(line[2])
  File "<string>", line 0

      

Could you help me? thank

What0s

+1


source to share


5 answers


Let me explain a little what you are doing here.

If you write:

NumPFCs = eval(line[2])

      

evaluation procedure:

  • take the second character of the string, i.e. quote ''
  • eval this quote as python expression which is a bug.

If you write this instead:

NumPFCs = eval(line)[2]

      



then the order of evaluation is:

  • eval a string creating a python list
  • take the second element of this list, which is a single character string: "0"
  • a string cannot be compared to a number; this is also a mistake.

In your terms, you want to do the following:

NumPFCs = eval(eval(line)[2])

      

or, slightly better, compare NumPFC to the line:

if NumPFCs == "0":

      

but the ways that can go wrong are almost countless. You should forget about eval

and try other methods: string splitting, regex, etc. Others have already provided some suggestions and I'm sure what will follow.

+1


source


Your question is hard to read, but using eval is definitely not a good idea. Or just do a direct string comparison:

line=TcsLine.split(",")
if line[2] == "0":
    print line

      

or use int



line=TcsLine.split(",")
if int(line[2]) == 0:
    print line

      

Either way, your bad data will fail you.

I would also recommend reading PEP 8 .

0


source


There are several problems in your code segment:

  • you are making the assumption that there are always at least 3 elements in the list
  • eval will throw an exception if the string is invalid python
  • you say you want the second item, but you are accessing the 3rd item.

This is a safer way to do it

line=TcsLine.split(",")
if len(line) >=3 and line[2].rfind("0") != -1:
     print line

      

0


source


I would recommend using a regex to capture all the options for how 0 can be specified: with double quotes, without quotes, with single quotes, with extra spaces outside the quotes, with spaces inside quotes, how you want to handle square brackets, etc.

0


source


There are many ways to skin a cat :)

Before we begin, don't use eval on strings that are not yours , so if the string ever left your program; that is, it remained in a file sent over the network, someone might send something nasty. And if someone can, you can be sure that someone will.

And you can preview your data format. Putting strings like ["baa", "beee", "0", "\ n"] in the file doesn't make much sense to me.

The first and easiest way would be to just remove stuff you don't need and compare the strings. This will work as long as the '0'-string always looks the same and you are not really after the integer value 0, just the character pattern:

TcsLine = '["baa","beee","0"]'

line = TcsLine.strip('[]').split(",")
if line[2] == '"0"':
   print line

      


The second method will be similar to the first, except that we pass a numeric string to an integer, giving the integer value you were looking for (but printing the string "without all the quotes"):

TcsLine = '["baa","beee","0"]'

line = [e.strip('"') for e in TcsLine.strip('[]').split(",")]
NumPFCs = int(line[2])
if NumPFCs==0:
   print line

      


Could it be that the string is actually a json array? Then I would probably go simplejson to parse it correctly if I was running Python <2.6 or just importing json to Python> = 2.6. Then draw the resulting "0" -jet to an integer, as in the previous example.

TcsLine = '["baa","beee","0"]'

#import json # for >= Python2.6
import simplejson as json # for <Python2.6

line = json.loads(TcsLine)
NumPFCs = int(line[2])
if NumPFCs==0:
   print line

      

0


source







All Articles