Reputation: 99
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"
What I am missing in this code. The if condition does not return true.
Upvotes: 1
Views: 81
Reputation: 5391
You need to clean your input by using the strip()
function
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]']
then you can do what you are doing
for i in range(len(string_list)):
print string_list[i]
if string_list[i] == 'pool_name':
print "here"
Upvotes: 0
Reputation: 19274
Your strings end in '\n'
, which you are not checking for:
for i in range(len(string_list)):
print string_list[i]
if string_list[i] == 'pool_name\n':
print "here"
Or, strip
the string to remove the '\n'
, and proceed in checking how you were:
for i in range(len(string_list)):
print string_list[i]
if string_list[i].strip() == 'pool_name':
print "here"
As such:
>>> 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]
>>>
Upvotes: 0
Reputation: 2740
for i in string_list:
if i.strip() == 'pool_name':
print "here"
would be a more pythonic way to do it.
Or maybe just
if 'pool_name\n' in string_list:
print "here"
Upvotes: 0
Reputation: 52181
You are missing a \n
in your statement
All your strings in the list have a \n
at the end
for i in range(len(string_list)):
print string_list[i]
if string_list[i] == 'pool_name\n': #Missing here
print "here"
Output after changing the statement
[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]
Upvotes: 2
Reputation: 5844
In your list the value is 'pool_name\n'
, not 'pool_name'
(difference of a \n
char, a newline). Either do:
if string_list[i] == 'pool_name\n':
Or:
if string_list[i].strip() == 'pool_name':
Upvotes: 0
Reputation: 20369
Try this
if string_list[i].strip() == 'pool_name':
Because
>>>>"pool_name\n".strip() # this will strip out \n, \r, \t, " ",
"pool_name"
OR
if string_list[i] == 'pool_name\n':
Upvotes: 2