将ASCII更改为字符串中的字符

时间:2018-04-15 17:50:56

标签: python python-3.x ascii python-3.6

我正在尝试将字符串中的ASCII值转换为普通字符,但无论我怎么做,我都会得到像

这样的输出
this\032is\032my\032\output\000

我需要

this is my output

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我用

解决了这个问题
import re
def replace(match):
    return chr(int(match.group(1)))
aux = str(element.text)
regex = re.compile(r"\\(\d{1,3})")
new = regex.sub(replace, aux)

其中element.text是我需要转换的字符串

答案 1 :(得分:0)

\032是十进制数26的八进制数,因此它实际上不是空格字符,如果使用具有正确数字的十六进制值,则应该得到您想要的结果。 0x20 == 32以下

>>> s = 'this\x20is\x20my\x20\output\00'
>>> print(s)
this is my \output