New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
windows: UnicodeDecodeError: 'gbk' codec can't decode #4
Comments
首先感谢你的反馈,对给你带来的麻烦表示歉意。 这个问题应该和 f312d64 类似,是中文环境的 Windows 下读取文件时编码的问题。 我这边没有 Windows 的机器,我用 mac 做了一些简单的尝试,但没能复现问题,所以可能需要你进一步提供更多的信息来帮助确定 BUG 所在。@BackMountainDevil 麻烦可以看一下你的 具体的方法是:利用 VSCode 等编辑器打开文件,在右下角会有显示。例如下图的文件是 UTF-8 编码的: |
两个文件都是UTF-8编码,通过VS Code/记事本右下角的状状态栏都是UTF-8. |
我把模块里面的main代码进行了修改 code = code_file.read().decode(encoding='utf-8', errors='ignore')
code = code_file.read().decode(encoding='utf-8') 无论修改为哪一个,错误都是那样 code = code_file.read().decode(encoding='utf-8')
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 215: illegal multibyte sequence
code = code_file.read().decode(encoding='utf-8', errors='ignore')
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 215: illegal multibyte sequence |
哈哈,这个修改思路和我想的一样,但你的实现有点小问题。应该还需要多改一行代码: if __name__ == '__main__':
parser.add_argument('code_file', type=argparse.FileType('r'))
你试试 |
模块中读取的编码是cp936 == gbkdef main(code_file, field, inner):
print("code_file: ", code_file, "\nfield: ", field, "\ninner: ", inner)
code = code_file.read().decode(encoding='utf-8', errors='ignore')
print(code)
flowchart = Flowchart.from_code(code, field=field, inner=inner)
print(flowchart.flowchart())
## output
code_file: <_io.TextIOWrapper name='cube.py' mode='r' encoding='cp936'>
field:
inner: True 但是无论指定encoding为cp936还是gbk都会报错 cube.py编码确实是utf-8path = "D://Documents//CAU\Lion//repositiries//Python//pyopengl//beginner//cube.py"
with open(path, 'r', encoding='utf') as text:
words = text.read()
print(words) 无论是在VS Code里运行还是powershell运行都是正确运行的,但是如果指定为gbk或者不设置encoding就会报错 |
open 的文档:https://docs.python.org/3/library/functions.html#open 里面说不指定 encoding 的话默认用 platform dependent 的编码。而据我了解,中文的 Windows 系统使用 GBK 编码。所以不指定 encoding 的话,就是用 GBK 去解码。所以无法解码 UTF-8 的文件。 |
我在 c7410be 里添加了一些自动检测文本编码的代码。应该可以解决你的问题了。但我仍然没有借到 Windows 电脑做测试😂,你可以用 #4_file_decode_error 这个分支里的代码试试😂 |
裂开...格式修改为gbk格式就行了,sorry |
emmm,但是你不应该把编码改成 GBK 的,Python3 默认用 UTF-8 编码
试试这个:
|
|
this will be released as v0.1.1 Closes #4
这个问题在新发布的 v0.1.1 版本中应该解决了🎉,pip 更新即可使用。 $ pip3 install -U pyflowchart 再次感谢 @BackMountainDevil 🙏 P.S. 关于 PyFlowchart 的完整用法,敬请参考 README。 |
Env
Err
code
Exceptioin
另一个代码证明了不是code问题,也爆出了同样的错误,因此错误是由于该模块本身的问题...但是我目前的知识还不足以维护这个模块...
code2
The text was updated successfully, but these errors were encountered: