Python任意字符集转换

在python处理文本的过程中,经常会有文本字符集转换的情况,
而我们希望用一个方法,不用关心文本原本的字符集是什么样的,直接转换成想要的任何字符集就可以了。

方法一:

import chardet
def convert_encoding(data,new_coding=‘UTF-8‘):
  # 任意字符集转换
  encoding = chardet.detect(data)[‘encoding‘]
  if new_coding.upper() != encoding.upper():
    data  = data.decode(encoding,data).encode(new_coding)
  return data 

方法二:

import icu
def convert_encoding2(data,new_coding=‘UTF-8‘):

  encoding = icu.CharsetDetector(data).detect().getName()
  # encoding = chardet.detect(content)[‘encoding‘]
  if new_coding.upper() != encoding.upper():
    # data  = data.decode(encoding,data).encode(new_coding)
    data = unicode(data,coding).encode(new_coding)
  return data 

方法三:

import cchardet
def convert_encoding3(data,new_coding=‘UTF-8‘):
  encoding = cchardet.detect(data)[‘encoding‘]
  if new_coding.upper() != encoding.upper():
    data  = data.decode(encoding,data).encode(new_coding)
  return data 

使用方法:

此处使用方法一

#转换成utf-8
convert_encoding(data,‘utf-8‘)

#转抱成GBK
convert_encoding(data,‘gbk‘)

#转抱成GB2312
convert_encoding(data,‘gbk‘)

原文地址:http://blog.51cto.com/yangrong/2130811

时间: 2024-08-27 04:08:43

Python任意字符集转换的相关文章

libiconv字符集转换库在C#中的使用

<libiconv字符集转换库使用方法>一文中说到了libiconv可以实现不同字符集的转换.比如GBK转BIG5等.在项目中因为需要,找到这个库.可是这个库在C#中没有很好的支持.不过,想着既然是C++的库,那只要动态加载DLL的接口就好了.可是调用并不顺利,传进去的IntPtr或者byte数组总是拿不到数据.后面回到了C++的方式去调用,几经调试,总算找到了原因. 是iconv接口在转换完成后,指针的位置往后移了.而在C#中调用DLL后回来的指针,已经是移动后的,所以拿不到所要的数据. 经

Python 将pdf转换成txt(不处理图片)

上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要.查找了很多资料,在linux下要将doc转换成txt确实有难度,所以考虑先将pdf转换成txt. 师兄推荐使用PDFMiner来处理,尝试了一番,确实效果不错,在此和大家分享. PDFMiner 的简介:PDFMiner is a tool for extracting information from PDF documents.

python将文字转换成图片

现在又很多工具能将文字转换成图片,这样就可以发送长微博,其实python实现这很容易的,主要就是用到了pygame模块 import os import pygame from pygame.locals import * pygame.init() text = u"这是一段测试文本,test 123." font = pygame.font.SysFont('SimHei', 14) ftext = font.render(text, True, (0, 0, 0), (255,

python的强制转换(当出现 not supported between instances of &#39;str&#39; and &#39;int&#39; 的错误时)

当我们编程时,有时会出现如下错误:TypeError: '>' not supported between instances of 'str' and 'int' 如下图: 这是因为input()返回的数据类型是str类型,不能直接和整数进行比较,必须先把str转换成整型,使用int()方法:age = int(input ("请输入你的年龄:")) 改正之后为: 这样程序就达到了预期的效果了 python的强制转换(当出现 not supported between inst

妙用python之编码转换

转自i春秋 文章难易度:★★ 知识点:python.编码转换 前 言 在日常渗透,漏洞挖掘,甚至是CTF比赛中,会遇到各种编码,常常伴随着这些编码之间的各种转换.记得刚入门那个时候,自己处理编码转换问题往往是"百度:url解码.base64加密.hex--",或者是使用一款叫做"小葵多功能转换工具"的软件,再后来直接上Burpsuite的decoder功能,感觉用的还挺好的.不过,也遇到些问题:在线转换效率低(搜索占去了2/3的时间).两款工具存在一些小问题,比如b

python把汉字转换成拼音实现程序

python把汉字转换成拼音实现程序 文章一个简洁干的汉字转拼音 程序,复制下载就可以直接使用,有需要的同学可以参考一下下. #coding:utf-8 #基于python2.6 table = 'a,-20319;ai,-20317;an,-20304;ang,-20295;ao,-20292;ba,-20283;bai,-20265;ban,-20257;bang,-20242;bao,-20230;bei,-20051;ben,-20036;beng,-20032;bi,-20026;bi

iconv_open函数申请一个字符集转换的描述符

iconv_open函数 iconv_open函数 iconv_open——申请一个字符集转换的描述符 #include <iconv.h> iconv_t iconv_open(const char* tocode,const char* fromcode) 描述: iconv_open()函数申请一个转换描述符,转换字符序列从编码fromcode到编码tocode 转换描述符包含转换状态,调用iconv_open创建以后,转换状态处于初始状态,调用iconv函数以后改变转换描述符的转换状态

python中将字典转换成定义它的json字符串

Python的字典和JSON在表现形式上非常相似 #这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'], 'sub_dic': { 'sub_str': 'this is sub str', 'sub_list': [1, 2, 3] }, 'end': 'end' } //这是javascript中的一个JSON对象 json_obj = { 'str': 'this is a string',

python将文本转换成语音的代码

将写代码过程中经常用的一些代码片段备份一下,如下代码段是关于python将文本转换成语音的代码,应该是对小伙伴们有一些好处. # Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente# download installer file pyTTS-3.0.win32-py2.4.exe # and pywin32-204.win32-py2.4.exe at this date the latest