Remove trailing newline from the elements of a string list
You can either use a list comprehension
my_list = ['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
stripped = [s.strip() for s in my_list]
or alternatively use map()
:
stripped = list(map(str.strip, my_list))
In Python 2, map()
directly returned a list, so you didn't need the call to list. In Python 3, the list comprehension is more concise and generally considered more idiomatic.
How to remove \n from a list element?
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()
# split line...
Remove newline characters from a list
You can use str.strip()
to remove leading and trailing whitespace from a string. To apply it to each item in the list you can use a list comprehension:
lst = [item.strip() for item in lst]
or the map()
function:
lst = list(map(str.strip, lst))
As a side note, don't name your variable list
as it would shadow the built-in function.
Remove white spaces from the beginning of each string in a list
Use str.lstrip in a list comprehension:
my_list = [' a', ' b', ' c']
my_list = [i.lstrip() for i in my_list]
print(my_list) # ['a', 'b', 'c']
Remove \r from list
You can use str.strip
. '\r'
is a carriage return, and strip
removes leading and trailing whitespace and new line characters.
>>> l = ['\r1/19/2015', '1/25/2015\r']
>>> l = [i.strip() for i in l]
>>> l
['1/19/2015', '1/25/2015']
How to remove newline from end and beginning of every list element [python]
You can do that by stripping "\n" as follow
a = "\nhello\n"
stripped_a = a.strip("\n")
so, what you need to do is iterate through the list and then apply the strip on the string as shown below
res_1=[]
for i in res:
tmp=[]
for j in i:
tmp.append(j.strip("\n"))
res_1.append(tmp)
The above answer just removes \n from start and end. if you want to remove all the new lines in a string, just use .replace('\n"," ")
as shown below
res_1=[]
for i in res:
tmp=[]
for j in i:
tmp.append(j.replace("\n"))
res_1.append(tmp)
strip() & rstrip() not removing newline from elements in list
You can remove newlines with i.replace('\n', '')
Remove the newline character in a list read from a file
str.strip()
returns a string with leading+trailing whitespace removed, .lstrip
and .rstrip
for only leading and trailing respectively.
grades.append(lists[i].rstrip('\n').split(','))
Related Topics
Search for String in All Pandas Dataframe Columns and Filter
Problems with Pip Install Numpy - Runtimeerror: Broken Toolchain: Cannot Link a Simple C Program
How to Disable Logging on the Standard Error Stream
Python Memoising/Deferred Lookup Property Decorator
"Line Contains Null Byte" in CSV Reader (Python)
Python Webbrowser.Open() to Open Chrome Browser
When and How to Use the Builtin Function Property() in Python
Counting Repeated Characters in a String in Python
Python - 'Ascii' Codec Can't Decode Byte
Writing a Dict to Txt File and Reading It Back
How to Stop Flask Application Without Using Ctrl-C
Cost of Exception Handlers in Python
How to Sum Values in a Column That Match a Given Condition Using Pandas
How to Filter Rows in Pandas by Regex
Longest Increasing Subsequence
Why in Numpy 'Nan == Nan' Is False While Nan in [Nan] Is True
How to Plot Multiple Functions on the Same Figure, in Matplotlib