Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 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. """Demonstrate encoding errors.
  7. """
  8. #end_pymotw_header
  9.  
  10. import codecs
  11. import sys
  12.  
  13. error_handling = sys.argv[1]
  14.  
  15. text = u'pi: \u03c0'
  16.  
  17. try:
  18. # Save the data, encoded as ASCII, using the error
  19. # handling mode specified on the command line.
  20. with codecs.open('encode_error.txt', 'w',
  21. encoding='ascii',
  22. errors=error_handling) as f:
  23. f.write(text)
  24.  
  25. except UnicodeEncodeError, err:
  26. print 'ERROR:', err
  27.  
  28. else:
  29. # If there was no error writing to the file,
  30. # show what it contains.
  31. with open('encode_error.txt', 'rb') as f:
  32. print 'File contents:', repr(f.read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement