Advertisement
rfmonk

codecs_encodedfile.py

Jan 29th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 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. # I get an interesting result with this
  8. # it prints out utf8, it changes it to utf16
  9. # but it does not print back to utf8
  10.  
  11. from codecs_to_hex import to_hex
  12.  
  13. import codecs
  14. from cStringIO import StringIO
  15.  
  16. # Raw version of the original data.
  17. data = u'pi: \u03c0'
  18.  
  19. # Manually encode it as UTF-8.
  20. utf8 = data.encode('utf-8')
  21. print 'Start as UTF-8   :', to_hex(utf8, 1)
  22.  
  23. # Set up an output buffer, then wrap it as an EncodedFile.
  24. output = StringIO()
  25. encoded_file = codecs.EncodedFile(output, data_encoding='utf-8',
  26.                                   file_encoding='utf-16')
  27. encoded_file.write(utf8)
  28.  
  29. # Fetch the buffer contents as a UTF-16 encoded byte string
  30. utf16 = output.getvalue()
  31. print 'Encoded to UTF-16:', to_hex(utf16, 2)
  32.  
  33. # Set up another buffer with the UTF-16 data for reading,
  34. # and wrap it with another EncodedFile.
  35. buffer = StringIO(utf16)
  36. endcoded_file = codecs.EncodedFile(buffer, data_encoding='utf-8',
  37.                                    file_encoding='utf-16')
  38.  
  39. # Read the UTF-8 encoded version of the data.
  40. recoded = encoded_file.read()
  41. print 'Back to UTF-8    :', to_hex(recoded, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement