Remove 
 from python string

19,392

Solution 1

You can simply do

s = s.replace('\r\n', '\n')

to replace all occurrences of CRNL with just NL, which seems to be what you want.

Solution 2

buffer = "<text from your subprocess here>\r\n"
no_cr = buffer.replace("\r\n", "\n")

Solution 3

If they are at the end of the string(s), I would suggest to use:

buffer = "<text from your subprocess here>\r\n"
no_cr = buffer.rstrip("\r\n")

You can also use rstrip() without parameters which will remove whitespace as well.

Solution 4

replace('\r\n','\n') should work, but sometimes it just does not. How strange. Instead you can use this:

lines = buffer.split('\r')
cleanbuffer = ''
for line in lines: cleanbuffer = cleanbuffer + line
Share:
19,392
directedition
Author by

directedition

Updated on June 04, 2022

Comments

  • directedition
    directedition about 2 years

    When you run something through popen in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?