the default error handling is 'strictthe 'xmlcharrefreplaceerror handling policy is often useful way to embed international characters into ascii-encoded text on web pages for exampleif you output the unicode string 'jalape\ oby encoding it to ascii with 'xmlcharrefreplacehandlingbrowsers will almost always correctly render the output text as "jalapenoand not some garbled alternative to keep your brain from explodingencoded byte strings and unencoded strings should never be mixed together in expressions (for exampleusing to concatenatepython prohibits this altogetherbut python will silently go ahead with such operations by automatically promoting byte strings to unicode according to the default encoding setting this behavior is often source of surprising results or inexplicable error messages thusyou should carefully try to maintain strict separation between encoded and unencoded character data in your program unicode / when working with unicode stringsit is never possible to directly write raw unicode data to file this is due to the fact that unicode characters are internally represented as multibyte integers and that writing such integers directly to an output stream causes problems related to byte ordering for exampleyou would have to arbitrarily decide if the unicode character +hhll is to be written in "little endianformat as the byte sequence ll hh or in "big endianformat as the byte sequence hh ll moreoverother tools that process unicode would have to know which encoding you used because of this problemthe external representation of unicode strings is always done according to specific encoding rule that precisely defines how unicode characters are to be represented as byte sequence thusto support unicode /othe encoding and decoding concepts described in the previous section are extended to files the built-in codecs module contains collection of functions for converting byte-oriented data to and from unicode strings according to variety of different data-encoding schemes perhaps the most straightforward way to handle unicode files is to use the codecs open(filename [mode [encoding [errors]]]functionas followsf codecs open('foo txt',' ','utf- ','strict' codecs open('bar txt',' ','utf- 'reading writing this creates file object that reads or writes unicode strings the encoding parameter specifies the underlying character encoding that will be used to translate data as it is read or written to the file the errors parameter determines how errors are handled and is one of 'strict''ignore''replace''backslashreplace'or 'xmlcharrefreplaceas described in the previous section if you already have file objectthe codecs encodedfile(fileinputenc [outputenc [errors]]function can be used to place an encoding wrapper around it here' an examplef open("foo txt","rb"fenc codecs encodedfile( ,'utf- ' lib fl ff