一些倒霉的同事将一些数据保存到这样的文件中:
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_foo错误保存的格式转换为正确保存的字符串?
忽然笑
BIG阳
相关分类