大5编码到utf-8转换不成功
我正在尝试做一些非常基础的字符集转换,类似于iconv的功能,但不知道为什么不成功。我在用Python的解码和编码功能,但感觉好像漏掉了什么很基本的东西。
代码:
#!/usr/bin/python
import sys
if __name__ == "__main__":
if len(sys.argv) < 2:
print ("wrong input")
sys.exit(1)
fi = open(sys.argv[1], "r")
buf = fi.read()
fi.close()
print ("got input: \n{0}".format(buf))
buf.decode("big5", "strict").encode("utf8", "strict")
fo = open(sys.argv[2], "w")
fo.write(buf)
fo.close()
print ("changed: \n{0}".format(buf))
输入文件。hello.big5是通过用iconv将utf文件转换得到的。
[workspace] > cat hello.utf8
hello = 你好
[workspace] > cat hello.big5
hello = �A�n
执行时:
[workspace] > ./test.py hello.big5 out
got input:
hello = �A�n
changed:
hello = �A�n
有人能告诉我哪里出错了吗?