Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 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. """Detecting the BOM.
  7. """
  8. #end_pymotw_header
  9.  
  10. import codecs
  11. from codecs_to_hex import to_hex
  12.  
  13. # Look at the raw data
  14. with open('nonnative-encoded.txt', mode='rb') as f:
  15. raw_bytes = f.read()
  16.  
  17. print 'Raw :', to_hex(raw_bytes, 2)
  18.  
  19. # Re-open the file and let codecs detect the BOM
  20. with codecs.open('nonnative-encoded.txt',
  21. mode='rt',
  22. encoding='utf-16',
  23. ) as f:
  24. decoded_text = f.read()
  25.  
  26. print 'Decoded:', repr(decoded_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement