Advertisement
rfmonk

codecs_bom_detection.py

Jan 29th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. import codecs
  8. from codecs_to_hex import to_hex
  9.  
  10. # Look at the raw data
  11. with open('nonnative-encoded.txt', mode='rb') as f:
  12.     raw_bytes = f.read()
  13.  
  14. print 'Raw      :', to_hex(raw_bytes, 2)
  15.  
  16. # Reopen the file and let codecs detect the BOM
  17. with codecs.open('nonnative-encoded.txt',
  18.                  mode='rt',
  19.                  encoding='utf-16',
  20.                  ) as f:
  21.     decoded_text = f.read()
  22.  
  23. print 'Decoded:', repr(decoded_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement