はUnicodeDecodeError
を解決する方法を知ってはいけない:のpython、コーデック、file.writelines()、UnicodeDecodeError
私がファイルにテキストを書き込むことができないです - 文字â = '0xe2'
について>UnicodeDecodeError
を。確かに
1)â = '0xe2'
文字が
2)re.search
)が定義されているがfile.writelines(string)
3を書き込むための私がしようとしています文字列、中â
文字を見つけることができない、その文字列内に存在しません。ファイルオープン時のerrors='replace'
であるため、file.writelines()
は文字エラーについては不平を言ってはいけません。あなたが使用しているcodecs.open
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).
あなたは文字列が部分文字列を含んでいるかどうかをテストしたい場合は、ちょうど 'の代わりに' '0xe2ので、とにかく動作しません。この 're'もの、のS'では「\ XE2」を実行します。ただ、これを試してみてください'!=' \ xe2''。 – yak