[code.view]

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

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Create a file with non-native BOM.
     """
     #end_pymotw_header
     
     import codecs
     from codecs_to_hex import to_hex
     
     # Pick the non-native version of UTF-16 encoding
     if codecs.BOM_UTF16 == codecs.BOM_UTF16_BE:
         bom = codecs.BOM_UTF16_LE
         encoding = 'utf_16_le'
     else:
         bom = codecs.BOM_UTF16_BE
         encoding = 'utf_16_be'
     
     print 'Native order  :', to_hex(codecs.BOM_UTF16, 2)
     print 'Selected order:', to_hex(bom, 2)
     
     # Encode the text.
     encoded_text = u'pi: \u03c0'.encode(encoding)
     print '{:14}: {}'.format(encoding, to_hex(encoded_text, 2))
     
     with open('non-native-encoded.txt', mode='wb') as f:
         # Write the selected byte-order marker.  It is not included in the
         # encoded text because we were explicit about the byte order when
         # selecting the encoding.
         f.write(bom)
         # Write the byte string for the encoded text.
         f.write(encoded_text)
     

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

contact | logmethods.com