检测python中是否存在字符集 [英] Detect whether charset exists in python

查看:93
本文介绍了检测python中是否存在字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Python中检查给定字符集是否存在/已安装。
例如:

check('iso-8859-1')-> True

check('bla')-> False

Is it possible to check in Python whether a given charset exists/is installed. For example:
check('iso-8859-1') -> True
check('bla') -> False

推荐答案

您可以在编解码器中使用 lookup()函数模块。如果编解码器不存在,则会引发异常:

You can use the lookup() function in the codecs module. It throws an exception if a codec does not exist:

import codecs
def exists_encoding(enc):
    try:
        codecs.lookup(enc)
    except LookupError:
        return False
    return True
exists_encoding('latin1')

这篇关于检测python中是否存在字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆