我想从 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/