def getCoding(strInput): ''' 获取编码格式 ''' if isinstance(strInput, unicode): return "unicode" try: strInput.decode("utf8") return 'utf8' except: pass try: strInput.decode("gbk") return 'gbk' except: pass def tran2UTF8(strInput): ''' 转化为utf8格式 ''' strCodingFmt = getCoding(strInput) if strCodingFmt == "utf8": return strInput elif strCodingFmt == "unicode": return strInput.encode("utf8") elif strCodingFmt == "gbk": return strInput.decode("gbk").encode("utf8") def tran2GBK(strInput): ''' 转化为gbk格式 ''' strCodingFmt = getCoding(strInput) if strCodingFmt == "gbk": return strInput elif strCodingFmt == "unicode": return strInput.encode("gbk") elif strCodingFmt == "utf8": return strInput.decode("utf8").encode("gbk")
关键字词:
相关文章
- python 经常报错'module' object has no attribute 'X509_up_ref'
- python安装cv2 无法安装
- Python来识别字符串所属语言类型(langid 、langdetect)
- Python3 字典的复制与修改
- Python3 print 不换行打印教程
- Python3 bytes to string 字节码转字符串
- Scrapy Proxy Python下爬虫使用代理
- Scrapy 爬虫入门 内建设置参考 Python爬虫教程实战
- Scrapy 爬虫入门 Items 与 Item Pipeline Python爬虫教程实战
- Scrapy 爬虫入门 Spider 与 Selectors (选择器) Python爬虫教程实战