0
fPath = raw_input('File Path:')
counter = 0;
flag = 0;

with open(fPath) as f:
    content = f.readlines()

for line in content:
    if flag == 0 and line.find("WECS number") or \
    line.find("WECS number") or \
    line.find("WECS name") or \
    line.find("Manufacturer") or \
    line.find("Type") or \
    line.find("Nominal effect") or \
    line.find("Hub height") or \
    line.find("x (local)") or \
    line.find("y (local)") or \
    line.find("x (global)") or \
    line.find("y (global)"):

        if not line.find("y (global)"):
            print ("Alert Last Line!");
        else:
            print("Alert Line!");

For some reason, the code seems to be printing "Alert Line!" if a line is just "\n". My intent in creating the "if and or" structure was to ignore all lines not containing the strings listed in the line.find's. Something is going wrong here...

How can I fix this problem?

2
  • this might be better suited to codereview.stackexchange.com Commented Sep 5, 2012 at 22:26
  • 2
    One style hint: instead of content=f.readlines(); for line in content: just do for line in f:. Commented Sep 5, 2012 at 22:32

3 Answers 3

6

The string's .find() method returns -1 if the substring is not found. -1 is nonzero and so is considered true. This probably is not what you expected.

A more Pythonic way (since you don't care about the position of the string) is to use the in operator:

if "WECS number" in line:   # and so on

You can also use startswith() and endswith() where appropriate:

if line.startswith("WECS number"):

Finally, you can avoid all those backslashes simply by using parentheses to enclose the entire Boolean expression. If parentheses are open, Python keeps going to the next line.

if (condition1 or condition2 or
    condition3 or condition4):
Sign up to request clarification or add additional context in comments.

4 Comments

I'd probably write phrases = ("WECS number", "WECS name") and then if any(phrase in line for phrase in phrases) rather than chain all those ors. If it's really startswith that's wanted, that's even better: line.startswith(phrases).
I have to credit the first answer since both are very good and correct. Thank so much...
I love that syntax. Now I realize why I'm on Stack Overflow so much.
@sepoto: be warned that using in has one danger which sometimes trips people up: "cat" in "a catfish is not a feline" is true, or maybe more to the point "WECS number" in "WECS number of turtles" is true. Whenever you use in (or startswith, for that matter), be sure you don't have accidental false positives.
1

The string find() method returns -1 if the string is not found. -1 counts as true in a boolean context. So your if clauses are executing when you don't think they are. You'd be better off using if "blah" in line to just test whether the substring is present.

Comments

1

str.find returns -1 if it doesn't find the substring, and boolean(-1) == True, so line.find("WECS number") is always True except when the line starts with WECS number, in which case line.find("WECS name") is True.

You want:

fPath = raw_input('File Path:')

with open(fPath) as f:
  for line in f:
    if any(s in line for s in ("WECS number", "WECS name", "Manufacturer","Type",
                               "Nominal effect", "Hub height", "x (local)",
                               "y (local)", "x (global)", "y (global)",)):

        if "y (global)" in line:
            print("Alert Line!")
        else:
            print ("Alert Last Line!")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.