Python 字节对象存储在";repr格式“;作为b';foo';而不是将()编码为字符串--如何修复?
一些倒霉的同事将一些数据保存到如下文件中:Python 字节对象存储在";repr格式“;作为b';foo';而不是将()编码为字符串--如何修复?,python,python-3.x,unicode,character-encoding,Python,Python 3.x,Unicode,Character Encoding,一些倒霉的同事将一些数据保存到如下文件中: s = b'The em dash: \xe2\x80\x94' with open('foo.txt', 'w') as f: f.write(str(s)) 当他们应该使用 s = b'The em dash: \xe2\x80\x94' with open('foo.txt', 'w') as f: f.write(s.decode()) 现在foo.txt看起来像 b'The em-dash: \xe2\x80\x94'
s = b'The em dash: \xe2\x80\x94'
with open('foo.txt', 'w') as f:
f.write(str(s))
当他们应该使用
s = b'The em dash: \xe2\x80\x94'
with open('foo.txt', 'w') as f:
f.write(s.decode())
现在foo.txt
看起来像
b'The em-dash: \xe2\x80\x94'
而不是
The em dash: —
我已将此文件作为字符串读取:
with open('foo.txt') as f:
bad_foo = f.read()
现在,我如何将
bad\u foo
从错误保存的格式转换为正确保存的字符串?此代码在我的计算机中工作正常。但如果您仍然出现错误,这可能会对您有所帮助
with open('foo.txt', 'r', encoding="utf-8") as f:
print(f.read())
这段代码在我的计算机中工作正常。但如果您仍然出现错误,这可能会对您有所帮助
with open('foo.txt', 'r', encoding="utf-8") as f:
print(f.read())
你可以试试
你可以试试
如果您相信输入不是恶意的,则可以对断开的字符串使用ast.literal\u eval
import ast
# Create a sad broken string
s = "b'The em-dash: \xe2\x80\x94'"
# Parse and evaluate the string as raw Python source, creating a `bytes` object
s_bytes = ast.literal_eval(s)
# Now decode the `bytes` as normal
s_fixed = s_bytes.decode()
否则,您将不得不手动解析、删除或替换有问题的repr'ed转义。如果您相信输入不是恶意的,您可以对断开的字符串使用ast.literal\u eval
import ast
# Create a sad broken string
s = "b'The em-dash: \xe2\x80\x94'"
# Parse and evaluate the string as raw Python source, creating a `bytes` object
s_bytes = ast.literal_eval(s)
# Now decode the `bytes` as normal
s_fixed = s_bytes.decode()
否则,您必须手动解析并删除或替换有问题的重复转义。
。如果没有编码名称,解码就没有意义。不管怎样,你为什么要首先使用字节字符串?实现这一点的惯用方法是使用Unicode字符串,并让Python在写入文件时对其进行编码。@tripleee是其他人做的,我的任务是撤销它:)我怀疑没有比eval
更有用的方法可以建议撤销它。@tripleee这是一个自我回答。请参阅@shadowtalker,在提问页面的“发布问题”按钮下方有一个“回答您自己的问题”复选框,让您在比赛前获得答案;-)<没有编码名称,code>.decode
就没有意义。不管怎样,你为什么要首先使用字节字符串?实现这一点的惯用方法是使用Unicode字符串,并让Python在写入文件时对其进行编码。@tripleee是其他人做的,我的任务是撤销它:)我怀疑没有比eval
更有用的方法可以建议撤销它。@tripleee这是一个自我回答。请参阅@shadowtalker,在提问页面的“发布问题”按钮下方有一个“回答您自己的问题”复选框,让您在比赛前获得答案;-)否,问题是文件包含字节字符串的repr()
。否,问题是文件包含字节字符串的repr()
。