1

I have a list that is read from a text file that outputs:

['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']

I want to remove the \n from each element, but using .split() does not work on lists only strings (which is annoying as this is a list of strings).

How do I remove the \n from each element so I can get the following output:

['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

7 Answers7

2

old_list = [x.strip() for x in old_list]

old_list refers to the list you want to remove the \n from.

Or if you want something more readable:

for x in range(len(old_list)):
    old_list[x] = old_list[x].strip()

Does the same thing, without list comprehension.

strip() method takes out all the whitespaces, including \n.

But if you are not ok with the idea of removing whitespaces from start and end, you can do:

old_list = [x.replace("\n", "") for x in old_list]

or

for x in range(len(old_list)):
    old_list[x] = old_list[x].replace("\n", "")
Martín Nieva
  • 476
  • 1
  • 5
  • 13
  • 2
    Or just `.strip()` - newline character is whitespace and `strip` with no arguments strips whitespace only. And only from the beginning and the end of the string. – h4z3 Nov 22 '19 at 15:04
  • As for your edit, nope. 1. Do not name own variable like a build-in one. 2. You don't save the result of the replace. – h4z3 Nov 22 '19 at 15:04
  • @h4z3: Bare `strip()` is a nice idea, only if OP is ok with removal of whitespaces (if any) from the ends. – Austin Nov 22 '19 at 15:10
  • 1
    I never understand why people say list comprehensions are less readable. They look less like “traditional” code than the for-loop but, in my experience of maintaining Python code, this makes them more rather than less readable. – jez Nov 22 '19 at 15:20
  • It's just to make newbies' life easier. A newbie can understand the code better with traditional for loops. I think they are at least a bit more readable than list comprehensions. – Martín Nieva Nov 22 '19 at 15:39
0

Use:

[i.strip() for i in lines]

in case you don't mind to lost the spaces and tabs at the beginning and at the end of the lines.

0

Give this code a try:

lst = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']

for n, element in enumerate(lst):
    element = element.replace('\n', '')
    lst[n] = element

print(lst)
Arkenys
  • 343
  • 1
  • 11
0

do a strip but keep in mind that the result is not modifying the original list, so you will need to reasign it if required:

a = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
a = [path.strip() for path in a]
print a
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You can read the whole file and split lines using str.splitlines:

temp = file.read().splitlines()

if you still have problems go to this question where I got the answer from

How to read a file without newlines? answered Sep 8 '12 at 11:57 Bakuriu

Josh Harold
  • 103
  • 7
0

There are many ways to achieve your result.

Method 1: using split() method

l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.split('\n')[0] for i in l]

print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']

Method 2: using strip() method that removes leading and trailing whitespace

 l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.strip() for i in l]

print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']

Method 3: using rstrip() method that removes trailing whitespace

l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']
result = [i.rstrip() for i in l]

print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']

Method 4: using the method replace

l = ['/Users/myname/Documents/test1.txt\n', '/Users/myname/Documents/test2.txt\n', '/Users/myname/Documents/test3.txt\n']

result = [i.replace('\n', '') for i in l]

print(result) # ['/Users/myname/Documents/test1.txt', '/Users/myname/Documents/test2.txt', '/Users/myname/Documents/test3.txt']
codrelphi
  • 1,075
  • 1
  • 7
  • 13
0

Here is another way to do it with lambda:

cleannewline = lambda somelist : map(lambda element: element.strip(), somelist)

Then you can just call it as:

cleannewline(yourlist)
Darko
  • 89
  • 1
  • 7