Home > Net >  Ignore \n character when iterate over a list in python
Ignore \n character when iterate over a list in python

Time:05-23

I have a list like that


mylist
['\nms-0/0/0\n', '\nms-0/1/0\n', '\nms-0/2/0\n']

and I want to use every literal value of the list in a way that \n character is ignored:

Example:


for i in mylist:
     flows.xpath("//service-sfw-flow-count[interface-name=i]/flow-count//text()")

the value of "i" is not \nms-0/0/0\n but ms-0/0/0 , so I wonder if there is an option to use the literal value of every element of the list.

I've tried repr(i), but I got extra characters

"'\\nms-0/0/0\\n'"

Any idea ?

Regards

CodePudding user response:

Perhaps you can use

i.replace('\n', '')

to replace that characters with an empty string.

For example.

for i in mylist:
     new_i = i.replace('\n', '')
     flows.xpath("//service-sfw-flow-count[interface-name=i]/flow-count//text()")

On the other hand, if the i deflows.xpath is your variable, it may be advisable to make the following modification.

for i in mylist:
     new_i = i.replace('\n', '')
     flows.xpath(f"//service-sfw-flow-count[interface-name={new_i}]/flow-count//text()")

CodePudding user response:

try this:

mylist = [r'\nms-0/0/0\n', r'\nms-0/1/0\n', r'\nms-0/2/0\n']
print(mylist)

find more here

  • Related