Python How to get rid of '\ r' in a string?

I have an excel file that I converted to a text file with a list of numbers.

test = 'filelocation.txt' in_file = open(test,'r') for line in in_file: print line 1.026106236 1.660274766 2.686381002 4.346655769 7.033036771 1.137969254 a = [] for line in in_file: a.append(line) print a '1.026106236\r1.660274766\r2.686381002\r4.346655769\r7.033036771\r1.137969254' 

I would like to assign each value (in each row) a separate element in the list. Instead, a single element is created, separated by the \ r character. I'm not sure what it is, but why introduce them into the code?

I think I know a way to get rid of \ r from the string, but I want to fix the problem from the source

+4
source share
6 answers

To accept any of \r , \n , \r\n as a new line, you can use the file mode 'U' (universal new line):

 >>> open('test_newlines.txt', 'rb').read() 'a\rb\nc\r\nd' >>> list(open('test_newlines.txt')) ['a\rb\n', 'c\r\n', 'd'] >>> list(open('test_newlines.txt', 'U')) ['a\n', 'b\n', 'c\n', 'd'] >>> open('test_newlines.txt').readlines() ['a\rb\n', 'c\r\n', 'd'] >>> open('test_newlines.txt', 'U').readlines() ['a\n', 'b\n', 'c\n', 'd'] >>> open('test_newlines.txt').read().split() ['a', 'b', 'c', 'd'] 

If you want to get a numerical (float) array from a file; see Reading a line of a file into an array (pythonic)

+5
source

use rstrip() or rstrip('\r') if you are sure that the last character is always \r .

 for line in in_file: print line.rstrip() 

help str.rstrip() :

 S.rstrip([chars]) -> string or unicode Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping 

str.strip() removes both trailing and leading spaces.

+2
source

Using strip ()

you can remove carriage and newline from a line,
 line.strip() 

i.e.

 for line in in_file: a.append(line.strip()) print a 
0
source

To fix this, follow these steps:

 for line in in_file: a.append(line.strip()) 
0
source

.strip() lines to remove spaces you don't need:

 lines = [] with open('filelocation.txt', 'r') as handle: for line in handle: line = line.strip() lines.append(line) print line print lines 

Also, I would suggest using with ... notation to open a file. It cleans and closes the file automatically.

0
source

Firstly, I generally like @JF Sebastian's answer, but my usage example is closer to Python 2.7.1: How to open, edit and close a CSV file , since my line appeared from a text file, it was output from Excel as csv and, It was also introduced using the csv module. As indicated on this question:

as for 'rU' vs 'rb' vs ..., the csv files really need to be binary, so use 'rb'. However, it is not uncommon to have csv files from someone who copied it to notepad on windows, and later it was connected to some other file so that you have funky line endings. How you handle this depends on your file and your preferences. - @kalhartt January 23 at 3:57

I will stick with reading as "rb" as recommended in python docs . At the moment, I know that \ r inside the cell is the result of quirks related to using Excel, so I just create a global option to replace '\ r' with something else, which will be '\ n' for now, but may be later '' (empty string, not double quote) with a simple json change.

0
source

All Articles