将某个字符串插入数据库时,我遇到了非常令人沮丧的错误.它说:
Python无法解码字节字符,期待unicode"
经过大量的搜索,我看到我可以通过将我的字符串编码为Unicode来克服这个错误.我尝试首先解码字符串,然后以UTF-8格式对其进行编码.喜欢:
string = string.encode("utf8")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
'ascii' codec can't decode byte 0xe3 in position 6: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我一直在为这个错误而死!我如何解决它?
我正在尝试使用fileinput
模块的就地过滤功能来就地重写输入文件。
需要将编码(用于读写)设置为latin-1
并尝试传递openhook=fileinput.hook_encoded('latin-1')
给它,fileinput.input
但由于错误而受阻
ValueError: FileInput cannot use an opening hook in inplace mode
Run Code Online (Sandbox Code Playgroud)
仔细检查后,我发现fileinput
文档中明确说明了这一点:不能同时使用就位和openhook
我该如何解决?