unicodedata.normalize()/使用strip()、rstrip()和lstrip()/encode和decode 笔记(具体可看 《Python Cookbook》3rd Edition 2.9~2.11)
unicodedata.normalize()清理字符串
# normalize()的第一个参数指定字符串标准化的方式,分别有NFD/NFC
>>> s1 = 'Spicy Jalape\u00f1o'
>>> s2 = 'Spicy Jalapen\u0303o'
>>> import unicodedata
# NFC表示字符应该是整体组成(可能是使用单一编码)
>>> t1 = unicodedata.normalize('NFC', s1)
>>> t2 = unicodedata.normalize('NFC', s2)
>>> t1 == t2
True
# NFD表示字符应该分解为多个组合字符表示
>>> t1 = unicodedata.normalize('NFD', s1)
>>> t2 = unicodedata.normalize('NFD', s2)
>>> t1 == t2
True
注:Python中同样支持NFKC/NFKD,使用原理同上
combining()匹配文本上的和音字符
>>> s1
'Spicy Jalapeño'
>>> t1 = unicodedata.normalize('NFD', s1)
>>> ''.join(c for c in t1 if not unicodedata.combining(c)) # 去除和音字符
'Spicy Jalapeno'
使用strip()、rstrip()和lstrip()
>>> s = ' hello world \n'
# 去除左右空白字符
>>> s.strip()
'hello world'
# 去除右边空白字符
>>> s.rstrip()
' hello world'
# 去除左边空白字符
>>> s.lstrip()
'hello world \n'
>>> t = '-----hello====='
# 去除左边指定字段('-')
>>> t.lstrip('-')
'hello====='
# 去除右边指定字段('-')
>>> t.rstrip('=')
'-----hello'
# 值得注意的是,strip等不能够去除中间空白字符,要使用去除中间空白字符可以使用下面方法
>>> s = ' hello world \n'
# 使用replace()那么会造成"一个不留"
>>> s.replace(' ', '')
'helloworld\n'
# 使用正则
>>> import re
>>> re.sub(r'\s+', ' ', s)
' hello world '
关于translate()
# 处理和音字符
>>> s = 'pýtĥöñ\fis\tawesome\r\n'
>>> remap = {ord('\r'): None, ord('\t'): ' ', ord('\f'): ' '} # 构造字典,对应空字符
>>> a = s.translate(remap) # 进行字典转换
>>> a
'pýtĥöñ is awesome\n'
>>> import unicodedata
>>> import sys
>>> cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode) if unicodedata.combining(chr(c))) # 查找系统的和音字符,并将其设置为字典的键,值设置为空
>>> b = unicodedata.normalize('NFD', a) # 将原始输入标准化为分解形式字符
>>> b
'pýtĥöñ is awesome\n'
>>> b.translate(cmb_chrs)
'python is awesome\n'
# 将所有的Unicode数字字符映射到对应的ASCII字符上
# unicodedata.digit(chr(c)) # 将ASCII转换为十进制数字,再加上'0'的ASCII就对应了“0~9”的ASCII码
>>> digitmap = {c: ord('')+unicodedata.digit(chr(c)) for c in range(sys.maxunicode) if unicodedata.category(chr(c)) == 'Nd'} # (unicodedata.category(chr(c)) == 'Nd')表示系统“0~9”的Unicode字符
>>> len(digitmap)
610
>>> x = '\u0661\u0662\u0663'
>>> x.translate(digitmap)
''
关于I/O解码和编码函数
>>> a
'pýtĥöñ is awesome\n'
>>> b = unicodedata.normalize('NFD', a)
>>> b.encode('ascii', 'ignore').decode('ascii')
'python is awesome\n'
unicodedata.normalize()/使用strip()、rstrip()和lstrip()/encode和decode 笔记(具体可看 《Python Cookbook》3rd Edition 2.9~2.11)的更多相关文章
- 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
- 探究 encode 和 decode 的使用问题(Python)
很多时候在写Python程序的时候都要在头部添加这样一行代码 #coding: utf-8 或者是这样 # -*- coding:utf-8 -*- 等等 这行代码的意思就是设定同一编码格式为utf- ...
- python的str,unicode对象的encode和decode方法(转)
python的str,unicode对象的encode和decode方法(转) python的str,unicode对象的encode和decode方法 python中的str对象其实就是" ...
- 48-python基础-python3-字符串-常用字符串方法(六)-strip()-rstrip()-lstrip()
7-用 strip().rstrip()和 lstrip()删除空白字符 strip()字符串方法将返回一个新的字符串,它的开头或末尾都没有空白字符. lstrip()和 rstrip()方法将相应删 ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- 【python】python新手必碰到的问题---encode与decode,中文乱码[转]
转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...
- LeetCode Encode and Decode Strings
原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...
- Encode and Decode Strings
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- encode和decode
Python字符串的encode与decode研究心得乱码问题解决方法 为什么会报错“UnicodeEncodeError: 'ascii' codec can't encode characters ...
随机推荐
- python之pandas用法大全
python之pandas用法大全 更新时间:2018年03月13日 15:02:28 投稿:wdc 我要评论 本文讲解了python的pandas基本用法,大家可以参考下 一.生成数据表1.首先导入 ...
- G++ C++之区别
1.遇到精度用C++ 2.G++内存超限,C++过了 其他都用G++
- Too much thinking! Too much annoying.
I am now in great demand for an opportunity to yearn for, the ability to express myself, in a maximu ...
- 前端笔记 (3.JavaScript 1)
JavaScript 是属于网络的脚本语言! JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编程代码. JavaScript 插入 HTML 页面后, ...
- 2017第八届蓝桥杯C/C++ B组省赛-日期问题
标题:日期问题 小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都在1960年1月1日至2059年12月31日.令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的 ...
- ECONOMETRICS CHAPTER1
♣回归一词的来历 回归到中等,子辈较父辈的身高回归到中等. ♣回归的现代含义 研究一个因变量对解释变量的依赖关系.通过解释变量来估计和预测因变量. ♣回归分析的结果不一定意味着因果关系,必须诉诸理论思 ...
- C高级第二次PTA作业
6-7 删除字符串中数字字符 1.设计思路: (1)算法: 第一步:定义一个字符数组item,输入一个字符串赋给字符数组item.调用函数delnum, 第二步:在函数delnum中定义循环变量i=0 ...
- MATLAB的一些小经验,记下来,facilitate future work
[转载请注明出处]http://www.cnblogs.com/mashiqi 2016/03/28 0.杂.这个帖子(https://www.zhihu.com/question/24499729) ...
- Django之模板层-语法:{{ }}
模版语法的深度查询(.) views.py def index(request): name = 'name' lis = [1,2,3,4,5,6] dic = {"name": ...
- JavaBasic_01
计算机和编程语言 谷歌pagerank算法:给每一个网页有一个权值 被越多网页引用的网页越重要 被越重要的网页引用越重要 给每一个网页赋予权值,空网页权值为0 (马尔科夫链) 机器语言 汇编语言 高级 ...