¶ 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,py4zh
It 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 stri
 
if __name__=="__main__":
       # 命令行测试
       import sys
##      sys.setappdefaultencoding('unicode')
       if len(sys.argv) > 1:
               stri = sys.argv[1]
       else:
               stri = sys.stdin.read()
       print zh2utf8(stri)
       print 'encc:',encc</[email protected]></python-[email protected]></[email protected]>

§ 写于: Wed, 01 Feb 2006 | 永久链接;源文: rdf ,rss ,raw | 分类: /utility/py4zh §
[MailMe] [Print] Creative Commons License

作品Zoom.Quiet创作,采用知识共享署名-相同方式共享 2.5 中国大陆许可协议进行许可。 基于zoomquiet.org上的作品创作。