Do not know how to resolve the UnicodeDecodeError:
I am not able to write text to file --> UnicodeDecodeError about character â = '0xe2'.
1) â = '0xe2' character for sure does not exists in that string
2) re.search is not able to find â character in the string, which I am trying to write file.writelines(string)
3) there is defined errors='replace' in file opening, thus file.writelines() should not complain about character errors.
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).
'\xe2' in sinstead of thisrestuff, which wouldn't work anyway because'0xe2' != '\xe2'.