gpt4 book ai didi

python - Unicode解码错误: 'cp932' codec can't decode byte 0xfc

转载 作者:太空宇宙 更新时间:2023-11-04 02:43:17 29 4
gpt4 key购买 nike

import os

for root, dirs, files in os.walk('Path'):
for file in files:
if file.endswith('.c'):
with open(os.path.join(root, file)) as f:
for line in f:
if 'word' in line:
print(line)

得到错误

UnicodeDecodeError: 'cp932' 编解码器无法解码位置 6616 中的字节 0xfc:非法多字节序列

我认为文件需要 shift jis 编码。我可以只在开始时设置编码吗?我试过设置 以 open(os.path.join(root, file),'r',encoding='cp932') 作为 f:但得到了同样的错误

最佳答案

你可以传递 errors='ignore',但一定要检查你的文件的编码是什么。

open(os.path.join(root, file),'r', encoding='cp932', errors='ignore')

关于python - Unicode解码错误: 'cp932' codec can't decode byte 0xfc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855297/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com