0

I'm reading output from program like this

test_output = test_run.communicate(input=str.encode(input))[0]
output = test_output .decode('utf-8').strip()

and if program returns new line like this

>>> a
>>> b

it reads it like this(with repr())

>>> 'a\r\nb'

I tried removing line breaks with

output.replace(r"\r\n","")

but it's not working, if I print it like regular string it returns

>>> a
>>> b

and repr()

>>> 'a\r\nb'

How can I remove \r\n from my string?

ylm
  • 39
  • 1
  • 5
  • Also see [How can I remove (chomp) a newline in Python?](http://stackoverflow.com/q/275018) and [How to remove \n from a list element?](http://stackoverflow.com/q/3849509) – Bhargav Rao Feb 08 '16 at 18:32

1 Answers1

1

Don't use a regexp, the regexp doesn't do what you think it does:

>>> 'a\r\nb'.replace('\r\n', '')
'ab'

i.e. just remove the r in replace(r'\r\n', '')

Oin
  • 6,951
  • 2
  • 31
  • 55