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

10,879

Solution 1

To accepts any of \r, \n, \r\n as a newline you could use 'U' (universal newline) file mode:

>>> 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 numeric (float) array from the file; see Reading file string into an array (In a pythonic way)

Solution 2

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

for line in in_file:
    print line.rstrip()

help on 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 whitespaces.

Share:
10,879

Related videos on Youtube

O.rka
Author by

O.rka

I am an academic researcher studying machine-learning and microorganisms

Updated on September 16, 2022

Comments

  • O.rka
    O.rka over 1 year

    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 wanted to assign each value (in each line) to an individual element in the list. Instead it is creating one element separated by \r . i'm not sure what \r is but why is putting these into the code ?

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

  • jfs
    jfs over 11 years
    note: .rstrip() won't help because for line in in_file doesn't recognize \r as a newline on the OPs machine so line may contain multiple \r inside, try: '1\r2\r'.rstrip()