不知道如何解決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