处理编码的包。

encoding-tools的Python项目详细描述


编码工具

pipeline statuscoverage reportPyPI - Python Version

本模块旨在提供一个包装器来处理python中的编码。

功能

将str编码为字节

fromencoding_toolsimportTheSoCalledGreatEncoderencoder=TheSoCalledGreatEncoder()encoder.load_str('hellò')encoder.encode('latin-1')encoded_string=encoder.encoded_data

是的,这比简单的'hellò'.encode('latin-1')复杂得多,但它处理编码错误。 默认情况下,如果遇到错误,它将回退到ascii。

fromencoding_toolsimportTheSoCalledGreatEncoderencoder=TheSoCalledGreatEncoder()encoder.load_str('cœur')# œ is not supported by latin-1encoder.encode('latin-1')encoded_string=encoder.encoded_data# equals to b'coeur' 

如果要强制ascii转换,可以通过指定force_ascii=True的 正在调用.encode()

将字节解码到str

fromencoding_toolsimportTheSoCalledGreatEncoder,GuessEncodingFailedExceptionencoder=TheSoCalledGreatEncoder()encoder.load_bytes(b'hell\xf2')try:encoder.decode()exceptGuessEncodingFailedExceptionase:# Deal with itraiseValueError('Wrong input...')fromedecoded_string=encoder.decoded_data# equals to 'hellò'encoding=encoder.encoding# equals to 'ISO-8859-1'

解码器将为您猜测编码使用伟大的chardet库。您也可以提供编码 如果知道何时加载数据:.load_bytes(b'hell\xf2', encoding='latin-1')

要更改数据编码,请按以下方式进行:

fromencoding_toolsimportTheSoCalledGreatEncoder,GuessEncodingFailedExceptionencoder=TheSoCalledGreatEncoder()encoder.load_bytes(b'hell\xf2')# latin-1try:encoder.decode()exceptGuessEncodingFailedExceptionase:# Deal with itraiseValueError('Wrong input...')fromeencoder.encode('utf-8')encoded_string=encoder.encoded_data# equals to b'hell\xc3\xb2'encoding=encoder.encoding# equals to 'utf-8'

路线图

  • 处理解码错误
  • 支持更多编码(测试套件)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用Eclipse Helios for Web项目   java指定行。作为编译器设置的分隔符?   osm映射中带有osmdroid的java标记   java Android ClassNotFoundException在路径:DexPathList上未找到类   java从j2me调用GWT方法   更新到java 8后的aspectj问题   如何仅从根元素java,com提取文本内容。滴虫软件。是的。html   安卓公司。谷歌。火基。数据库DatabaseException:未能转换java类型的值。将字符串改为长字符串   从java中的方法返回arraylist变量   java如果将引用重新分配给同步块内的锁对象,会是什么?   java下载。使用spring mvc的资源文件中的xlsx文件   java Eclipse提供了什么工具来运行Eclipse中未包含的程序?   spring boot Java泛型如何读取传递给泛型方法的类型上的字段或调用方法   Javacard中的javaecdsa签名