简体   繁体   中英

Convert a string with escape sequences to their original character representation in python

Let's assume I have a program that receives inputs that I can't really control. The following variables are fed in as is (we can't change the inputs here):

a = "C:\temp"
b = "C:\games"
c = "Hello World"
d = "\t"

Unfortunately, Python will misinterpret things and put in escape sequences:

In [138]: a[2]
Out[138]: '\t'

In [139]: b[2]
Out[139]: '\\'

In [140]: d[0]
Out[140]: '\t'

Let's assume the answer has already been found. It should do the following:

def answer(x):
    pass #TODO: your code goes here

Desired outputs:

In [200]: answer(a)[2]
Out [201]: '\\'

In [202]: answer(a)[3]
Out [203]: 't'

In [204]: answer(b)[2]
Out [205]: '\\'

In [206]: answer(b)[3]
Out [207]: 'g'

In [208]: answer(c)
Out [209]: 'Hello World'

I've already tried using the ast module and also using decode, to no avail:

In [144]: import ast

In [145]: ast.literal_eval(a)
  File "<unknown>", line 1
    C:  emp
     ^
SyntaxError: invalid syntax

Or with decode:

In [147]: a.decode('string-escape')[2]
Out[147]: '\t'

Solve for answer()

Edit: "\\", not "\\" in the [204] example

You have to use encode not decode :

>>> "\t".encode('string-escape')
'\\t'

You can convert this strings into their representation using repr , then strip ' and " and take the char:

>>> a = 'C:\temp'
>>> a[2]
'\t'
>>> repr(a).strip('\'"')[2]
'\\'

answer for that matter, would look like

def answer(x): return repr(x).strip('\'"')

If i understand your question, you should convert your string into their original representation using repr then use str.partition() like this example:

>>> a = 'C:\temp'
>>> repr(a).partition('\\')

("'C:", '\\', "temp'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM