Please help on print string that contains 'tab' and 'newline'

Peter Otten __peter__ at web.de
Sun Jan 28 03:57:01 EST 2018


Jason Qian via Python-list wrote:

> HI
> 
>    I have a string that contains \r\n\t
> 
>    [Ljava.lang.Object; does not exist*\r\n\t*at
>    [com.livecluster.core.tasklet
> 
> 
>    I would like to  print it as :
> 
> [Ljava.lang.Object; does not exist
>      tat com.livecluster.core.tasklet
> 
>   How can I do this in python print ?

Assuming the string contains the escape sequences rather than an actual
TAB, CR or NL you can apply codecs.decode():

>>> s = r"[Ljava.lang.Object; does not exist\r\n\tat 
com.livecluster.core.tasklet"
>>> print(s)
[Ljava.lang.Object; does not exist\r\n\tat com.livecluster.core.tasklet
>>> import codecs
>>> print(codecs.decode(s, "unicode-escape"))
[Ljava.lang.Object; does not exist
        at com.livecluster.core.tasklet

Note that this will decode all escape sequences that may occur in a string 
literal
in Python, e. g.

>>> codecs.decode(r"\x09 \u03c0 \N{soft ice cream}", "unicode-escape")
'\t π 🍦'

and will complain when the string is not a valid Python string literal:

>>> codecs.decode(r"\x", "unicode-escape")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 
0-1: truncated \xXX escape

If you need more control you can build your own conversion routine:

import re

lookup = {
    "r": "\r",
    "n": "\n",
    "t": "\t",
}

def substitute(match):
    group = match.group(1)
    return lookup.get(group, group)

def decode(s):
    return re.sub(r"\\(.)", substitute, s)

s = decode("alpha\\n \\xomega\\\\")
print(s)
print(repr(s))





More information about the Python-list mailing list