[code.view]

[top] / python / PyMOTW / codecs / codecs_encode_error.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Demonstrate encoding errors.
     """
     #end_pymotw_header
     
     import codecs
     import sys
     
     error_handling = sys.argv[1]
     
     text = u'pi: \u03c0'
     
     try:
         # Save the data, encoded as ASCII, using the error
         # handling mode specified on the command line.
         with codecs.open('encode_error.txt', 'w',
                          encoding='ascii',
                          errors=error_handling) as f:
             f.write(text)
             
     except UnicodeEncodeError, err:
         print 'ERROR:', err
         
     else:
         # If there was no error writing to the file,
         # show what it contains.
         with open('encode_error.txt', 'rb') as f:
             print 'File contents:', repr(f.read())
     

[top] / python / PyMOTW / codecs / codecs_encode_error.py

contact | logmethods.com