Python中的字符串等价/比较(String Equivalence/Comparison in Python)

我似乎无法正确比较python中的字符串,在if条件下比较它的正确方法是什么?

Testfile = "test.txt" with open(TestFile) as testSet: line = testSet.read().rstrip('\n') if "#*#*# ham" or "#*#*# spam" in line: print line

我的test.txt像这样:

#*#*# ham foo bar bar foo bar bar bar bar #*#*# ham foo bar bar foo bar foo bar foo foo bar bar #*#*# spam foo bar foo foo bar barfoo bar foo foo bar bar #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# ham foo bar foo foo #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# spam bar foo bar foo foo bar #*#*# spam bar bar foo foo

I can't seem to compare strings properly in python, what is the correct way to compare it in the if condition?

Testfile = "test.txt" with open(TestFile) as testSet: line = testSet.read().rstrip('\n') if "#*#*# ham" or "#*#*# spam" in line: print line

My test.txt something like looks like this:

#*#*# ham foo bar bar foo bar bar bar bar #*#*# ham foo bar bar foo bar foo bar foo foo bar bar #*#*# spam foo bar foo foo bar barfoo bar foo foo bar bar #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# ham foo bar foo foo #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# spam bar foo bar foo foo bar #*#*# spam bar bar foo foo

最满意答案

做:

Testfile = "test.txt" with open(TestFile) as testSet: for line in testSet: line = line.strip() if "#*#*# ham" in line or "#*#*# spam" in line: print line

而不是你在做什么。 您正在将整个文件读入行变量,代码的方式。

Do:

Testfile = "test.txt" with open(TestFile) as testSet: for line in testSet: line = line.strip() if "#*#*# ham" in line or "#*#*# spam" in line: print line

instead of what you are doing. You are reading the whole file into the line variable, the way your code is.

Python中的字符串等价/比较(String Equivalence/Comparison in Python)

我似乎无法正确比较python中的字符串,在if条件下比较它的正确方法是什么?

Testfile = "test.txt" with open(TestFile) as testSet: line = testSet.read().rstrip('\n') if "#*#*# ham" or "#*#*# spam" in line: print line

我的test.txt像这样:

#*#*# ham foo bar bar foo bar bar bar bar #*#*# ham foo bar bar foo bar foo bar foo foo bar bar #*#*# spam foo bar foo foo bar barfoo bar foo foo bar bar #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# ham foo bar foo foo #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# spam bar foo bar foo foo bar #*#*# spam bar bar foo foo

I can't seem to compare strings properly in python, what is the correct way to compare it in the if condition?

Testfile = "test.txt" with open(TestFile) as testSet: line = testSet.read().rstrip('\n') if "#*#*# ham" or "#*#*# spam" in line: print line

My test.txt something like looks like this:

#*#*# ham foo bar bar foo bar bar bar bar #*#*# ham foo bar bar foo bar foo bar foo foo bar bar #*#*# spam foo bar foo foo bar barfoo bar foo foo bar bar #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# ham foo bar foo foo #*#*# spam foo bar foo foo bar bar foo foo bar bar #*#*# spam bar foo bar foo foo bar #*#*# spam bar bar foo foo

最满意答案

做:

Testfile = "test.txt" with open(TestFile) as testSet: for line in testSet: line = line.strip() if "#*#*# ham" in line or "#*#*# spam" in line: print line

而不是你在做什么。 您正在将整个文件读入行变量,代码的方式。

Do:

Testfile = "test.txt" with open(TestFile) as testSet: for line in testSet: line = line.strip() if "#*#*# ham" in line or "#*#*# spam" in line: print line

instead of what you are doing. You are reading the whole file into the line variable, the way your code is.