如何解释此错误“UnicodeDecodeError:'ascii'编解码器无法解码位置164中的字节0xe2:序号不在范围(128)” [英] how to interpret this error "UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 164: ordinal not in range(128)"
问题描述
我试图在python 3中运行以下代码:
def func(file):
对于os.listdir(cwd)中的文件:
如果file.endswith('。html'):
f = open(file,r +)
text = re.sub(r' cat','',f.read())
f.close()
f = open(file,w)
f.write(text)
f。关闭()
文件= os.listdir(cwd)
func(文件)
然后我得到错误文件/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py,第26行解码返回codecs.ascii_decode(input,self.errors)[0]
UnicodeDecodeError:'ascii'编解码器无法解码位置164的字节0xe2:序号不在范围内(128)
来源全部为英文,所以不知道这里发生了什么?
非常感谢您提前!
找到一种解决方法:
f = open(file,encoding ='utf-8',mode =r +)
< c $ c> f = open(file,encoding ='utf-8',mode =w)
/ p>
I'm trying to run the below code in python 3:
def func(file):
for file in os.listdir(cwd):
if file.endswith('.html'):
f = open(file, "r+")
text = re.sub(r'cat',' ', f.read())
f.close()
f = open(file, "w")
f.write(text)
f.close()
file = os.listdir(cwd)
func(file)
Then I got the error File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 164: ordinal not in range(128)
The source is all in English, so not sure what is going on with here?
Thank you very much in advance!
found a way to solve this:
f = open(file, encoding = 'utf-8', mode = "r+")
f = open(file, encoding = 'utf-8', mode = "w")
it worked.
这篇关于如何解释此错误“UnicodeDecodeError:'ascii'编解码器无法解码位置164中的字节0xe2:序号不在范围(128)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!