¶ zh2utf8.py
2006-02-01 23:23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | """Auto converter encodings to utf8#tags utility,py4zhIt will test utf8,gbk,big5,jp,kr to converter发件人: HuangJiahua <[email protected]> 邮送域: googlegroups.com收件人: "python.cn" <[email protected]>日期: 2006-1-16 上午12:11主题: Re: 请问怎样得到一个文件的编码?"""#!/usr/bin/python# coding:UTF-8# Author: Huang Jiahua <[email protected]>#测试的编码类型encc=''def zh2utf8(stri): """Auto converter encodings to utf8 It will test utf8,gbk,big5,jp,kr to converter""" global encc for c in ('utf-8', 'gbk', 'big5', 'jp','euc_kr','utf16','utf32'): encc = c try: return stri.decode(c).encode('utf8') except: pass encc = 'unk' return striif __name__=="__main__": # 命令行测试 import sys## sys.setappdefaultencoding('unicode') if len(sys.argv) > 1: stri = sys.argv[1] else: stri = sys.stdin.read() print zh2utf8(stri) |




