区分类型type与编码chardet.detect(),以及中文字符的编码统一处理原理

本文主要是介绍区分类型type与编码chardet.detect(),以及中文字符的编码统一处理原理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

**
总结1:只有字符类型即str类型的才有编码,整数及其他没有编码,检测编码会报错
总结2:编码:自动编码规则—根据编译环境自动为字符编码,通常,英文字母或数字会编码成ascii,中文会编码成utf-8;
总结3:解码,对于数字和英文字母解码,decode用ascii和utf8解码均能成功,解码成ascii;对于中文字符的解码,decode只能用utf-8解码,ascii解码会报错,中文字符解码后也为ascii;
总结4:加'u'表示Unicode编码,Unicode编码既包括utf-8,也包括ascii,未加u默认中文编码为'utf-8',加了u之后变成英文编码
总结5:对于中文字符的处理——将字符转换成str,再判断str是否是unicode编码,如果是再将其解码成ascii;
**
**只有字符类型即str类型的才有编码,整数及其他没有编码,检测编码会报错**
stra = "中", 则使用type(stra)的结果是<type 'str'>,表明为ascii类型字符串;
strb = u"中", 则使用type(strb)的结果是<type 'unicode'>,表明为unicode类型字符串。
判断类型用type和判断编码用chardet.detect(a)
In[37]: p=1
In[38]: type(1)
Out[38]: int
In[39]: chardet.detect(p)
Traceback (most recent call last):File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_codeexec code_obj in self.user_global_ns, self.user_nsFile "<ipython-input-39-ca1457f42cf7>", line 1, in <module>chardet.detect(p)File "/usr/lib/python2.7/dist-packages/chardet/__init__.py", line 24, in detectu.feed(aBuf)File "/usr/lib/python2.7/dist-packages/chardet/universaldetector.py", line 64, in feedaLen = len(aBuf)
TypeError: object of type 'int' has no len()#**根据编译环境自动为字符编码,通常,英文或数字会编码成ascii,中文会编码成utf-8**
**数字编码**p='1' 
In[41]: chardet.detect(p)
Out[41]: {'confidence': 1.0, 'encoding': 'ascii'}
In[42]: p=u'1'
In[43]: chardet.detect(p)
Out[43]: {'confidence': 1.0, 'encoding': 'ascii'}
**字母编码数字**
In[46]: p1=u'a'
In[47]: chardet.detect(p1)
Out[47]: {'confidence': 1.0, 'encoding': 'ascii'}
In[48]: p1='a'
In[49]: chardet.detect(p1)
Out[49]: {'confidence': 1.0, 'encoding': 'ascii'}
**含有中文字符的编码**
In[50]: p2='a好21'
In[51]: chardet.detect(p2)
Out[51]: {'confidence': 0.505, 'encoding': 'utf-8'}
In[52]: p2=u'a好21'
In[53]: chardet.detect(p2)
Out[53]: {'confidence': 1.0, 'encoding': 'ascii'}
############################
解码:
**数字解码**
decode('utf-8')成ascii;
In[57]: chardet.detect(str1)
Out[57]: {'confidence': 1.0, 'encoding': 'ascii'}
In[58]: str1.decode('utf-8')
Out[58]: u'1'
In[59]: chardet.detect(str1)
Out[59]: {'confidence': 1.0, 'encoding': 'ascii'}
**中文解码**
In[62]: str2='哈哈'
In[63]: chardet.detect(str2)
Out[63]: {'confidence': 0.7525, 'encoding': 'utf-8'}
In[64]: str2.decode('utf-8')
Out[64]: u'\u54c8\u54c8'
In[65]: chardet.detect(str2.decode('utf-8'))
Out[65]: {'confidence': 1.0, 'encoding': 'ascii'}
In[72]: str2.decode('ascii')
Traceback (most recent call last):File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_codeexec code_obj in self.user_global_ns, self.user_nsFile "<ipython-input-72-d98396330a85>", line 1, in <module>str2.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
中文字符用ascii解码会报错;
**字母解码**
In[66]: str3 = 'num'
In[67]: chardet.detect(str3)
Out[67]: {'confidence': 1.0, 'encoding': 'ascii'}
In[68]: str3.decode('utf-8')
Out[68]: u'num'
In[69]: chardet.detect(str3)
Out[69]: {'confidence': 1.0, 'encoding': 'ascii'}
In[70]: str3.decode('ascii')
Out[70]: u'num'
In[71]: chardet.detect(str3)
Out[71]: {'confidence': 1.0, 'encoding': 'ascii'}
############################通过spark读取的csv,里面的中文字符用法注意,需要加u实现,形如:if big_hy_name in [u'石油加工、炼焦及核燃料加工业',......],
注意:sqlContext.sql语句中可以直接用where big_hy_name = '石油加工、炼焦及核燃料加工业',不需要加u;encode()对字符串编码
decode()对字符串解码
**对于一个正常的未加u的字符串,其编码方式是由默认的编码设置决定的;**
小案例:
######################################
**情况1 —— python中文字符串加'u'和不加'u'**
加'u'表示Unicode编码,Unicode编码既包括utf-8,也包括ascii,未加u默认中文编码为'utf-8',加了u之后变成英文编码ascii;
In[12]: a='石油加工、炼焦及核燃料加工业'
In[13]: import chardet
In[14]: chardet.detect(a)
Out[14]: {'confidence': 0.99, 'encoding': 'utf-8'}
In[15]: a=u'石油加工、炼焦及核燃料加工业'
In[16]: chardet.detect(a)
Out[16]: {'confidence': 1.0, 'encoding': 'ascii'}
###############################################
**情况2 —— utf-8编码成ascii**
a='石油加工、炼焦及核燃料加工业',正常;
In[29]: chardet.detect(a)
Out[29]: {'confidence': 0.99, 'encoding': 'utf-8'}
In[30]: a.decode('utf-8')
Out[30]: u'\u77f3\u6cb9\u52a0\u5de5\u3001\u70bc\u7126\u53ca\u6838\u71c3\u6599\u52a0\u5de5\u4e1a'
In[31]: a
Out[31]: '\xe7\x9f\xb3\xe6\xb2\xb9\xe5\x8a\xa0\xe5\xb7\xa5\xe3\x80\x81\xe7\x82\xbc\xe7\x84\xa6\xe5\x8f\x8a\xe6\xa0\xb8\xe7\x87\x83\xe6\x96\x99\xe5\x8a\xa0\xe5\xb7\xa5\xe4\xb8\x9a'
In[32]: k1=a.decode('utf-8')
In[33]: k1
Out[33]: u'\u77f3\u6cb9\u52a0\u5de5\u3001\u70bc\u7126\u53ca\u6838\u71c3\u6599\u52a0\u5de5\u4e1a'
In[34]: chardet.detect(k1)
Out[34]: {'confidence': 1.0, 'encoding': 'ascii'}
可以看出,字符串默认是utf-8,或许是reload(sys),sys.setdefaultencoding('utf8')的原因,因此需要对其解码,用a.decode('utf-8'),a仍然不变,需要重新命名一个新的变量k1存储k1=a.decode('utf-8'),解码之后为ascii码;k1.encode('utf-8')
Out[36]: '\xe7\x9f\xb3\xe6\xb2\xb9\xe5\x8a\xa0\xe5\xb7\xa5\xe3\x80\x81\xe7\x82\xbc\xe7\x84\xa6\xe5\x8f\x8a\xe6\xa0\xb8\xe7\x87\x83\xe6\x96\x99\xe5\x8a\xa0\xe5\xb7\xa5\xe4\xb8\x9a'可以看出,将解码为ascii码的字符k1,重新编码成utf-8,k1.encode('utf-8'),是可以的;
######################
**情况3 —— ascii编码成utf-8  ,报错**
In[27]: a='石油加工、炼焦及核燃料加工业'
In[28]: a.encode('ascii')
Traceback (most recent call last):File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_codeexec code_obj in self.user_global_ns, self.user_nsFile "<ipython-input-28-bb580b290ae2>", line 1, in <module>a.encode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)
In[29]: chardet.detect(a)
Out[29]: {'confidence': 0.99, 'encoding': 'utf-8'}
可以看出:如果将utf-8编码的字符用ascii编码,会报如下错:UnicodeDecodeError;
###############################
总结:spark中csv读取中文编码默认是以英文字符读取的,即ascii编码;
**字符编码:对于中文字符的处理——将字符转换成str,再判断str是否是unicode编码,如果是再将其解码成ascii,**
def __check_the_encode(self, str1):if not isinstance(str1, str):str1 = str(str1)if not isinstance(str1, unicode):str1 = str1.decode('utf-8')return str1
为什么这样做能解决问题?
这样做目的是为了将行业代码编码统一,如果行业代码不是str那么强转为str,可以避免行业代码为0120这样的情况,转为str后为'120',又由于映射大小行业的字典中是这样的{'1':'120'},因此,其在编译环境下,通常将数字字符编码成ascii码,因此,需要进一步将上述str解码decode('utf-8')成ascii;
In[57]: chardet.detect(str1)
Out[57]: {'confidence': 1.0, 'encoding': 'ascii'}
In[58]: str1.decode('utf-8')
Out[58]: u'1'
In[59]: chardet.detect(str1)
Out[59]: {'confidence': 1.0, 'encoding': 'ascii'}In[62]: str2='哈哈'
In[63]: chardet.detect(str2)
Out[63]: {'confidence': 0.7525, 'encoding': 'utf-8'}
In[64]: str2.decode('utf-8')
Out[64]: u'\u54c8\u54c8'
In[65]: chardet.detect(str2.decode('utf-8'))
Out[65]: {'confidence': 1.0, 'encoding': 'ascii'}In[66]: str3 = 'num'
In[67]: chardet.detect(str3)
Out[67]: {'confidence': 1.0, 'encoding': 'ascii'}
In[68]: str3.decode('utf-8')
Out[68]: u'num'
In[69]: chardet.detect(str3)
Out[69]: {'confidence': 1.0, 'encoding': 'ascii'}
In[70]: str3.decode('ascii')
Out[70]: u'num'
In[71]: chardet.detect(str3)
Out[71]: {'confidence': 1.0, 'encoding': 'ascii'}###############################
def hy_map_nan(big_hy_name):if big_hy_name in [u'石油加工、炼焦及核燃料加工业',u'电力、热力、燃气及水生产和供应业',u'化学原料及化学制品制造业',u'医药制造业和销售',u'砖瓦、石材等建筑材料制造',u'广告业',u'批发和零售业',u'其它',u'综合',u'贸易、进出口',u'邮政业',u'其他商务服务业',u'电气机械及器材制造业',u'水利管理业',u'管道运输业',u'专用化学产品制造',u'居民服务、修理和其他服务业',u'人力资源服务',u'体育、娱乐业']:return 'nan'else:return big_hy_namedef shixin_risk_type(spark, sc):from pyspark.sql.functions import udffrom pyspark.sql.types import StringTypesqlContext = SQLContext(sparkContext=sc)# df1 = sqlContext.read.csv(readpath + 'new_update_hy_risk_rank_proportion.csv', header=True)df1 = sqlContext.read.csv(readpath + 'hy_chuli_shixin_db_dataset.csv', header=True)df1.show()df1.createOrReplaceTempView('b1')df12 = sqlContext.sql("select * from b1 where big_hy_name = '石油加工、炼焦及核燃料加工业'")df12.show()spark.stop()df1.createOrReplaceTempView('b1')df12=sqlContext.sql("select * from b1 order by big_hy_name")df12.show()spark.stop()df = sqlContext.read.csv(readpath + 'Orderby_shixin_decision_tree_prob_info.csv', header=True)risk_type_map = udf(prob_map_to_rank, StringType())bighy_map_nan = udf(hy_map_nan, StringType())df2 = df.withColumn("risk_type", risk_type_map(df['shixin_prob']))df3 = df2.withColumn("new_bighy_name", bighy_map_nan(df2['big_hy_name']))

这篇关于区分类型type与编码chardet.detect(),以及中文字符的编码统一处理原理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/517885

相关文章

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

Java 队列Queue从原理到实战指南

《Java队列Queue从原理到实战指南》本文介绍了Java中队列(Queue)的底层实现、常见方法及其区别,通过LinkedList和ArrayDeque的实现,以及循环队列的概念,展示了如何高效... 目录一、队列的认识队列的底层与集合框架常见的队列方法插入元素方法对比(add和offer)移除元素方法

C# WebAPI的几种返回类型方式

《C#WebAPI的几种返回类型方式》本文主要介绍了C#WebAPI的几种返回类型方式,包括直接返回指定类型、返回IActionResult实例和返回ActionResult,文中通过示例代码介绍的... 目录创建 Controller 和 Model 类在 Action 中返回 指定类型在 Action

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

SQL 注入攻击(SQL Injection)原理、利用方式与防御策略深度解析

《SQL注入攻击(SQLInjection)原理、利用方式与防御策略深度解析》本文将从SQL注入的基本原理、攻击方式、常见利用手法,到企业级防御方案进行全面讲解,以帮助开发者和安全人员更系统地理解... 目录一、前言二、SQL 注入攻击的基本概念三、SQL 注入常见类型分析1. 基于错误回显的注入(Erro