Skip to content Skip to sidebar Skip to footer

How To Remove "\n" In A List Of Lists (python)

I have a giant list of lists that I imported from a file like this: letters = [] for i in range(len(string)): let = [] for j in range(7): line = infile.readline()

Solution 1:

Wherever your current code has a call to .readline(), make it .readline().rstrip('\n') instead.

Solution 2:

Try:

line = infile.readline()
line = line.strip('\n')
let = let + [line]

Solution 3:

If you want to remove \n from the last element only, use this:

t[-1] = t[-1].strip()

If you want to remove \n from all the elements, use this:

t = map(lambda s: s.strip(), t)

You might also consider removing \n before splitting the line:

line = line.strip()

Post a Comment for "How To Remove "\n" In A List Of Lists (python)"