Search code examples
pythonpython-3.xstringstdoutcarriage-return

How to remove carriage return characters from string as if it was printed?


I would like to remove all occurrences of \r from a string as if it was printed via print() and store the result in another variable.

Example:

>>> s = "hello\rworld"
>>> print(s)
world

In this example, how do I "print" s to a new variable which then contains the string "world"?

Background: I am using the subprocess module to capture the stdout which contains a lot of \r characters. In order to effectively analyze the string I would like to only have the resulting output.


Solution

  • Using a regex:

    import re
    
    s = "hello\rworld"
    out = re.sub(r'([^\r]+)\r([^\r\n]+)',
                 lambda m: m.group(2)+m.group(1)[len(m.group(2)):],
                 s)
    

    Output: 'world'

    More complex example:

    import re
    
    s = "hello\r..\nworld"
    
    out = re.sub(r'([^\r]+)\r([^\r\n]+)',
                 lambda m: m.group(2)+m.group(1)[len(m.group(2)):],
                 s)
    

    Output:

    ..llo
    world