一些倒霉的同事將一些數據保存到這樣的文件中: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錯誤保存的格式轉換為正確保存的字符串?
3 回答

忽然笑
TA貢獻1806條經驗 獲得超5個贊
您可以嘗試文字 eval
from ast import literal_eval
test = r"b'The em-dash: \xe2\x80\x94'"
print(test)
res = literal_eval(test)
print(res.decode())

BIG陽
TA貢獻1859條經驗 獲得超6個贊
如果您相信輸入不是惡意的,則可以ast.literal_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()
否則,您將不得不手動解析并刪除或替換有問題的重復轉義。
舉報
0/150
提交
取消