Removing \r\n from a Python list after importing with readlines -
I have saved a list of ticker symbols in a text file as follows:
mmm ABT ABBV ANF .... Then I use the symbols in the Python list:
share = open (text file) .Redline () However, when I look at the list in the window that is going to exit the end line line which I do not want:
list: ['MMM \ r \ n', 'ABT' R \ n ',' abbv \ r \ n ',' anf \ r \ n ',' ace \ r \ n ',' acn \ r \ n ',' atti 'r \ n', 'adby \ r \ N ',' ADT \ r \ n ',' AMD \ r \ n ',' AES \ r \ n ', ..... Does anyone remove these unwanted characters? The easiest way to recommend?
This basically works by redunes you post-process it stock = [x.rstrip (for stock in x) but I do not like to use the readlines share = open (textfile) .read (). Splitlines () / Pre> or better than:
with open textfile (f: stocks = f.read (). Splitlines () (This almost certainly will not make any difference here, but using context managers to explicitly close the file objects is a good habit).
Comments
Post a Comment