[code.view]

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

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Writing Unicode data to a file.
     """
     #end_pymotw_header
     
     from codecs_to_hex import to_hex
     
     import codecs
     import sys
     
     encoding = sys.argv[1]
     filename = encoding + '.txt'
     
     print 'Writing to', filename
     with codecs.open(filename, mode='wt', encoding=encoding) as f:
         f.write(u'pi: \u03c0')
     
     # Determine the byte grouping to use for to_hex()
     nbytes = { 'utf-8':1,
                'utf-16':2,
                'utf-32':4,
                }.get(encoding, 1) 
     
     # Show the raw bytes in the file
     print 'File contents:'
     with open(filename, mode='rt') as f:
         print to_hex(f.read(), nbytes)
     

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

contact | logmethods.com