import sys
import chardet
f = open(sys.argv[1], 'r')
c = f.read()
f.close()
result = chardet.detect(c)
encoding = result['encoding']
print 'Detected', encoding, 'confidence:', result['confidence']
c = c.decode(encoding).encode('utf8')
f = open(sys.argv[2], 'w')
f.write(c);
f.close();

比如这个程序保存为 autoconv.py
运行 python autoconv.py 源文件 目标文件
就可以将源文件自动转换为 UTF-8 并写入目标文件

不过刚刚测试了一下,如果字数太少的话,还是有挺大的机会会判断错的吧


2012/5/6 HeChi-Lau <[email protected]>

>
> 嘿嘿,python仅限于hello world.
>
> 好像2和3语法很大不同啊.现在我服务器上是2.4.能否写个范例?谢了!
>
> -------------- 下一部分 --------------
> 一个HTML附件被移除...
> URL: <
> https://lists.ubuntu.com/archives/ubuntu-zh/attachments/20120506/e63d2ad5/attachment.html
> >
> --
> ubuntu-zh mailing list
> [email protected]
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-zh
>
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: 
<https://lists.ubuntu.com/archives/ubuntu-zh/attachments/20120506/5eee8c32/attachment.html>
-- 
ubuntu-zh mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-zh

回复