We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

ckyang74 on 05/15/08


Tagged

pythonbig5utf8encoding


Versions (?)


read a big5 file and print to stdout in utf8


Published in: Python 


  1. import sys
  2. from codecs import EncodedFile
  3.  
  4. ## EncodedFile "wraps" stdout so that it always takes a "from_encoding" string and
  5. ## convert it to a "to_encoding" string
  6. from_encoding = 'big5'
  7. to_encoding = 'utf8'
  8. sys.stdout = EncodedFile(sys.stdout, from_encoding, to_encoding)
  9.  
  10. ## read a file in big5 encoding
  11. f = file("foo.html", "r")
  12. str = f.read()
  13.  
  14. ## print the content of the file in utf8
  15. sys.stdout.write(str) # you can also use "print str"

Report this snippet 

You need to login to post a comment.