不知道如何解决UnicodeDecodeError
:蟒蛇,编解码器,file.writelines()的UnicodeDecodeError
我不能写文本文件 - >UnicodeDecodeError
有关字符â = '0xe2'
。
1)â = '0xe2'
汉字不在该字符串
2)re.search
无法找到字符串,而我试图在â
字符写file.writelines(string)
3)有定义存在errors='replace'
在文件打开,因此file.writelines()
不应该抱怨字符错误。
File=codecs.open(fname, 'w','utf-8', errors='replace')
lines=smart_str(lines, 'utf-8', strings_only=False, errors='replace')
# lines is 'some webpage text after BeautifulSoup.prettify which does not contain letter â ='0xe2', which is converted with Django smart_str to string'
FileA.writelines(lines) #gives UnicodeDecodeError : 'ascii' codec can't decode byte 0xe2 in position 9637: ordinal not in range(128).
myre = re.compile(r'0xe2', re.UNICODE) # letter â = '0xe2'
print re.search(myre, lines) #gives None
linessub=myre.sub('', lines)
print re.search(myre, linessub) #gives None
FileA.writelines(lines) #gives UnicodeDecodeError : 'ascii' codec can't decode byte 0xe2 in position 9637: ordinal not in range(128).
如果你想测试一个字符串是否包含一个子,只是在's',而不是这个're'东西“\ XE2”,这无论如何不能工作,因为''0xe2 '!='\ xe2''。 – yak