LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1157|回复: 2

Python: 把不确定编码的文本统一转换为 unicode 的

[复制链接]
发表于 2005-8-26 12:04:01 | 显示全部楼层 |阅读模式
Python

转换不确定编码的文本

依次尝试不同的编码,出现异常就尝试下一个
[php]
def zhtounicode(str):
        for c in ('utf-8', 'gbk', 'big5', 'jp', 'kr'):   
                try:
                        return str.decode(c)
                except:
                        pass
        return str

[/php]


原讨论:http://www.linuxforum.net/forum/ ... ;fpart=1#Post574179
发表于 2005-8-26 13:12:04 | 显示全部楼层
汗,我碰到的问题是从网上抓下的网页,想从中截取某部分tag,才发现编码有错误,比如,明明知道是gbk编码,但是直接用iconv转换成utf-8就出错。不知道huahua有没有自动纠错的脚本。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-8-26 17:02:05 | 显示全部楼层
sfatsdu:
可以用用 autogb  , 她可以自动识别编码,在字数 > 100 时候,几乎没有识别错误的

在包 zh-autoconvert 里
zh-autoconvert - Chinese HZ/GB/BIG5/UNI/UTF7/UTF8 encodings auto-converter



  1. hua@hua:~$ autogb -h
  2. autogb: invalid option -- h

  3. Usage: autogb [-OPTION] < input > output
  4. Now autoconvert can only judge gb/big5/hz encoding, so if you want to
  5. do other conversion, you must set the option --input.  Without --input,
  6. the program will guess the encoding of the input.
  7. OPTION is one of (case insensitive):
  8. -i encoding, --input encoding:                 Set the input encoding
  9. -o encoding, --output encoding:                        Set the output encoding
  10. The encoding should be gb, big5, hz, uni, utf7 or utf8.
复制代码


如  autogb -o utf8 < old.html > new.html
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表