Posted By

ckyang74 on 05/15/08


Tagged

python utf8 encoding big5


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.