Python 如果条件未返回true

Python 如果条件未返回true,python,Python,我在这段代码中缺少了什么。if条件不返回true 试试这个 string_list = ['[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n', '[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_

我在这段代码中缺少了什么。if条件不返回true

试试这个

string_list = ['[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n', '[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n']
for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name':
        print "here"
因为

if string_list[i].strip() == 'pool_name':

试试这个

string_list = ['[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n', '[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n']
for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name':
        print "here"
因为

if string_list[i].strip() == 'pool_name':


在您的列表中,值是
'pool\u name\n'
,而不是
'pool\u name'
(字符的差异,换行符)。或者:

if string_list[i] == 'pool_name\n':
或:


在您的列表中,值是
'pool\u name\n'
,而不是
'pool\u name'
(字符的差异,换行符)。或者:

if string_list[i] == 'pool_name\n':
或:


您的对账单中缺少一个
\n

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name\n':   #Missing here
        print "here"
列表中的所有字符串末尾都有一个
\n

if string_list[i].strip() == 'pool_name':
更改语句后的输出

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name\n':   #Missing here
        print "here"

您的对账单中缺少一个
\n

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name\n':   #Missing here
        print "here"
列表中的所有字符串末尾都有一个
\n

if string_list[i].strip() == 'pool_name':
更改语句后的输出

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name\n':   #Missing here
        print "here"
这将是一种更具python风格的方式

或者只是

for i in string_list:
    if i.strip() == 'pool_name':
        print "here"   
这将是一种更具python风格的方式

或者只是

for i in string_list:
    if i.strip() == 'pool_name':
        print "here"   

字符串以
'\n'
结尾,您没有检查该字符串:

if 'pool_name\n' in string_list:
    print "here"
或者,
strip
字符串以删除
'\n'
,然后继续检查您的情况:

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name\n':
        print "here"
因此:

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i].strip() == 'pool_name':
        print "here"

字符串以
'\n'
结尾,您没有检查该字符串:

if 'pool_name\n' in string_list:
    print "here"
或者,
strip
字符串以删除
'\n'
,然后继续检查您的情况:

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i] == 'pool_name\n':
        print "here"
因此:

for i in range(len(string_list)):
    print string_list[i]
    if string_list[i].strip() == 'pool_name':
        print "here"

您需要使用
strip()
函数清理输入

>>> foo()
[pool]

pool_name

here
node_name ip_address port

node_name ip_address port

node_name ip_address port

[/pool]

[pool]

pool_name

here
node_name ip_address port

node_name ip_address port

node_name ip_address port

[/pool]
>>>
然后你可以做你正在做的事情

string_list = ['[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n', '[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n']

string_list =   [i.strip() for i in string_list]

string_list
['[pool]', 'pool_name', 'node_name ip_address port', 'node_name ip_address port', 'node_name ip_address port', '[/pool]', '[pool]', 'pool_name', 'node_name ip_address port', 'node_name ip_address port', 'node_name ip_address port', '[/pool]']

您需要使用
strip()
函数清理输入

>>> foo()
[pool]

pool_name

here
node_name ip_address port

node_name ip_address port

node_name ip_address port

[/pool]

[pool]

pool_name

here
node_name ip_address port

node_name ip_address port

node_name ip_address port

[/pool]
>>>
然后你可以做你正在做的事情

string_list = ['[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n', '[pool]\n', 'pool_name\n', 'node_name ip_address port\n', 'node_name ip_address port\n', 'node_name ip_address port\n', '[/pool]\n']

string_list =   [i.strip() for i in string_list]

string_list
['[pool]', 'pool_name', 'node_name ip_address port', 'node_name ip_address port', 'node_name ip_address port', '[/pool]', '[pool]', 'pool_name', 'node_name ip_address port', 'node_name ip_address port', 'node_name ip_address port', '[/pool]']

在索引处同时使用索引和元素值时,强烈建议使用
enumerate
)在索引处同时使用索引和元素值时,强烈建议使用
enumerate