Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- # usage: strict - ignore - replace
- import codecs
- import sys
- from codecs_to_hex import to_hex
- error_handling = sys.argv[1]
- text = u'pi: \u03c0'
- print 'Original :', repr(text)
- # Save the data with one encoding
- with codecs.open('decode_error.txt', 'w', encoding='utf-16') as f:
- f.write(text)
- # Dump the bytes from the file
- with open('decode_error.txt', 'rb') as f:
- print 'File contents:', to_hex(f.read(), 1)
- # Try to read the data with the wrong encoding
- with codecs.open('decode_error.txt', 'r',
- encoding='utf-8',
- errors=error_handling) as f:
- try:
- data = f.read()
- except UnicodeDecodeError, err:
- print 'ERROR:', err
- else:
- print 'Read :', repr(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement