remove line break from each element in python

一笑奈何 提交于 2020-01-21 09:44:17

问题


I open a text file, now each element in the text file is separated by a line break. when I use readlines() It keeps this line break in the list, this is after each element so as it looks like this [zebra\n, ant\n,] i was wondering whether there is a easy function or piece of coding that I could use to just strip all the elements of this line break!

this is the code i have written so far

filelist=file.readlines()
file.close()
return filelist

回答1:


try:

filelist = [line.rstrip('\n') for line in file]


来源:https://stackoverflow.com/questions/14535619/remove-line-break-from-each-element-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!