Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #
  4. # Copyright (c) 2010 Doug Hellmann. All rights reserved.
  5. #
  6. """Create a file with nonnative BOM.
  7. """
  8. #end_pymotw_header
  9.  
  10. import codecs
  11. from codecs_to_hex import to_hex
  12.  
  13. # Pick the nonnative version of UTF-16 encoding
  14. if codecs.BOM_UTF16 == codecs.BOM_UTF16_BE:
  15. bom = codecs.BOM_UTF16_LE
  16. encoding = 'utf_16_le'
  17. else:
  18. bom = codecs.BOM_UTF16_BE
  19. encoding = 'utf_16_be'
  20.  
  21. print 'Native order :', to_hex(codecs.BOM_UTF16, 2)
  22. print 'Selected order:', to_hex(bom, 2)
  23.  
  24. # Encode the text.
  25. encoded_text = u'pi: \u03c0'.encode(encoding)
  26. print '{:14}: {}'.format(encoding, to_hex(encoded_text, 2))
  27.  
  28. with open('nonnative-encoded.txt', mode='wb') as f:
  29. # Write the selected byte-order marker. It is not included
  30. # in the encoded text because the byte order was given
  31. # explicitly when selecting the encoding.
  32. f.write(bom)
  33. # Write the byte string for the encoded text.
  34. f.write(encoded_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement