python - 如何在Python中读取解释后的数据字符串?

标签 python hex byte

我想从 Python 文件中读取所有字符串。示例文件(/tmp/s.py):

s = '{\x7f5  x'

现在我尝试从脚本中读取字符串:

import re
find_str = re.compile(r"'(.+?)'")

for line in open('/tmp/s.py', 'r'):
    all_strings = find_str.findall(line)
    print(all_strings) # outputs ['{\\x7f5  x']

但我希望字符串(在本例中为转义十六进制表示形式的字节)不被转义。我想处理/tmp/s.py 文件中的数据,并获取带有解释的\x7f 字节的字符串,而不是文字\x7f,它现在表示为\\x7f。

我该怎么做?

最佳答案

您可以使用 unicode_escape 编解码器来解码字符串,就像 Python 读取字符串文字时的方式一样:

print(*[s.encode('latin1').decode('unicode_escape') for s in all_strings])

请注意,unicode_escape 只能从字节解码,而不能从文本解码。编解码器也仅限于 Latin-1 源代码,而不是默认的 UTF-8。

来自Text Encodings section Python codecs 模块:

unicode_escape

Encoding suitable as the contents of a Unicode literal in ASCII-encoded Python source code, except that quotes are not escaped. Decodes from Latin-1 source code. Beware that Python source code actually uses UTF-8 by default.

演示:

>>> s = r'{\x7f5  x'
>>> s
'{\\x7f5  x'
>>> s.encode('latin1').decode('unicode_escape')
'{\x7f5  x'

关于python - 如何在Python中读取解释后的数据字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31166678/

相关文章:

python - Pyomo: KeyError: "Index ' (0, 1, 1 )' is not valid for indexed component ' x_ijl'"

assembly - JMP 指令 - 十六进制代码

c++ - std::hex 保持值不变

加密期间 Java 内存不足错误

c++ - 从蓝牙地址读取 3 个字节?

python - 如何从数据列表制作直方图并使用 matplotlib 绘制它

python - Python 中枚举的枚举?

java - 使用公式 int unsignedByte =signedByte >= 0 将有符号字节转换为无符号字节?有符号字节 : 256 + signedByte;

python - 找出字符串编码的聪明方法?

c# - C# 中的 HexBinaryAdapter?