Removing \r\n from a Python list after importing with readlines

ぃ、小莉子 提交于 2019-11-30 14:36:08

问题


I have saved a list of ticker symbols into a text file as follows:

MMM
ABT
ABBV
ANF
....

Then I use readlines to put the symbols into a Python list:

stocks = open(textfile).readlines()

However, when I look at the list in it contains Windows end-of-line delimiter which I do not want:

list: ['MMM\r\n', 'ABT\r\n', 'ABBV\r\n', 'ANF\r\n', 'ACE\r\n', 'ACN\r\n', 'ACT\r\n', 'ADBE\r\n', 'ADT\r\n', 'AMD\r\n', 'AES\r\n', .....

Can someone suggest the easiest way to remove these unwanted characters?


回答1:


That's basically how readlines works. You could post-process it:

stocks = [x.rstrip() for x in stocks]

But I prefer not using readlines at all if I don't want EOL character(s), instead doing:

stocks = open(textfile).read().splitlines()

Or even better:

with open(textfile) as f:
    stocks = f.read().splitlines()

(it almost certainly won't make a difference here, but using context managers to explicitly close file objects is a good habit to get into)




回答2:


readlines() should never be used unless you know that the file is really small. For your application, it is better to use rstrip()

with open(filename, 'r') as f:
    for l in f:
        l = l.rstrip()
        # other operations. 



回答3:


You could replace \r\n with the empty string in a replace command.

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



回答4:


You could do it like this:

stocks = open(textfile).read().splitlines()


来源:https://stackoverflow.com/questions/24946640/removing-r-n-from-a-python-list-after-importing-with-readlines

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