'Ignore \n character when iterate over a list in python

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



Solution 1:[1]

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()")

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2