read a big5 file and print to stdout in utf8


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.