简体   繁体   English

简单的python IF语句不起作用?

[英]Simple python IF statement not working?

In my code, I am trying to check a specific line of a config file by using linecache to read the line and an if statement to see if it is True, but for some reason it refuses to work. 在我的代码中,我试图通过使用linecache读取该行以及一个if语句来检查配置文件的特定行,以查看它是否为True,但是由于某种原因它拒绝工作。

I simplified the code in order to test it. 我简化了代码以对其进行测试。

import linecache

d = linecache.getline('logconfig.dat', 2)

print(d)
if d == True:
    doeslock = True
else:
    doeslock = False

print(doeslock)

No matter what I try, print(d) will print True , and print(doeslock) will print False . 不管我尝试什么, print(d)都将输出True ,而print(doeslock)将输出False I even tried using letters and strings instead of bools. 我什至尝试使用字母和字符串代替布尔符号。 Still didnt work. 仍然没有工作。 What am I missing here? 我在这里想念什么?

Thanks in advance guys 在此先感谢大家

EDIT: 编辑:

When I used strings to do the comparison, I replaced the True in the file with ay and modified the if statement to see if the d variable is 'y' 当我使用字符串进行比较时,我用y替换了文件中的True ,并修改了if语句以查看d变量是否为“ y”

EDIT 2: 编辑2:

Ok guys I found the problem. 好的,我发现了问题。 Turns out the linecache returned both the line I want and the previous one for whatever reason. 事实证明,无论出于何种原因,linecache都返回了我想要的行和上一行。 I separated the config into two files and now it works fine. 我将配置分为两个文件,现在工作正常。 No idea what caused this to happen but oh well. 不知道是什么原因导致了这种情况的发生,但是很好。 Thanks for the help! 谢谢您的帮助!

When you read a line from a file, you get the whole line, including the newline character, as a string. 当您从文件中读取一行时,您会获得包括换行符在内的行作为字符串。 You should strip that off and then compare with another string: 您应该将其剥离,然后与另一个字符串进行比较:

if d.strip('\n') == "True":

The string is not the same as the boolean value. 该字符串与布尔值不同。 Consider: 考虑:

>>> d = 'True'
>>> print(d)
True
>>> if d == True:
...   doeslock = True
... else:
...   doeslock = False
... 
>>> print(doeslock)
False
>>> bool('False')
True

What you probably want is: 您可能想要的是:

import linecache

d = (linecache.getline('logconfig.dat', 2)).strip()

print(d)
doeslock = (d == 'True')

print(doeslock)

Also consider the following: 还请考虑以下事项:

>>> with open('randfile', 'w') as whatever:
...   whatever.write('y')
... 
>>> import linecache
>>> d = linecache.getline('randfile', 1)
>>> print(d)
y

>>> d == 'y'
False
>>> 'y' in d
True
>>> d.strip() == 'y'
True

linecache.getline returns a string, not a boolean. linecache.getline返回一个字符串,而不是布尔值。 So you have to compare d to "True" . 因此,您必须将d"True"进行比较。 You also may have to strip the result of getline in case there are spaces or newline. 如果有空格或换行符,您可能还必须剥离getline的结果。

>>> if d=="True":
...   print 'ok'
...
ok

尝试d.strip()=='True':可能是您有多余的空间或\\ n。

import linecache 导入线缓存

d = linecache.getline('logconfig.dat', 2) d = linecache.getline('logconfig.dat',2)

print(d) 打印(d)

if d: 如果d:

doeslock = True

else: 其他:

doeslock = False

print(doeslock) 打印(锁)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM