码迷,mamicode.com
首页 > 编程语言 > 详细

python2判断编码格式

时间:2019-02-23 10:28:07      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:get   编码   ==   span   NPU   编码格式   判断   bsp   tran   

 1 def getCoding(strInput):
 2     ‘‘‘
 3     获取编码格式
 4     ‘‘‘
 5     if isinstance(strInput, unicode):
 6         return "unicode"
 7     try:
 8         strInput.decode("utf8")
 9         return utf8
10     except:
11         pass
12     try:
13         strInput.decode("gbk")
14         return gbk
15     except:
16         pass
17         
18 def tran2UTF8(strInput):
19     ‘‘‘
20     转化为utf8格式
21     ‘‘‘
22     strCodingFmt = getCoding(strInput)
23     if strCodingFmt == "utf8":
24         return strInput
25     elif strCodingFmt == "unicode":
26         return strInput.encode("utf8")
27     elif strCodingFmt == "gbk":
28         return strInput.decode("gbk").encode("utf8")
29 
30 def tran2GBK(strInput):
31     ‘‘‘
32     转化为gbk格式
33     ‘‘‘
34     strCodingFmt = getCoding(strInput)
35     if strCodingFmt == "gbk":
36         return strInput
37     elif strCodingFmt == "unicode":
38         return strInput.encode("gbk")
39     elif strCodingFmt == "utf8":
40         return strInput.decode("utf8").encode("gbk")

 

python2判断编码格式

标签:get   编码   ==   span   NPU   编码格式   判断   bsp   tran   

原文地址:https://www.cnblogs.com/hushaojun/p/10421695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!