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 依赖关系
class Person: def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来 tools.run() print("很开心, 我能玩儿游戏了&quo ...
- python 基础 内置函数 和lambda表达式
1.把任意数值转化为字符串有两种方法. (1)str()用于将数值转化为易于人读的形式.print(str("我是中国人"))>>>我是中国人 (2)repr() ...
- Golang游戏服务器与skynet的个人直观比较
我对和GOLANG写MMO服务器的一些遐想: 1.沙盒(隔离性) SKYNET:原生LUA STATE作为沙盒, 进行服务器间隔离安全性高: 服务可以很容易的配置到不同节点之上. GO:估计用RECO ...
- PHP设计模式之观察者模式(转)
开篇还是从名字说起,“观察者模式”的观察者三个字信息量很大.玩过很多网络游戏的童鞋们应该知道,即便是斗地主,除了玩家,还有一个角色叫“观察者".在我们今天他谈论的模式设计中,观察者也是如此. ...
- 【集成学习】sklearn中xgboost模块的XGBClassifier函数
# 常规参数 booster gbtree 树模型做为基分类器(默认) gbliner 线性模型做为基分类器 silent silent=0时,不输出中间过程(默认) silent=1时,输出中间过程 ...
- 求割点 割边 Tarjan
附上一般讲得不错的博客 https://blog.csdn.net/lw277232240/article/details/73251092 https://www.cnblogs.com/colle ...
- 百度地图api开发:根据坐标获得地理描述地址
// 创建地理编码实例 var myGeo = new BMap.Geocoder(); // 根据坐标得到地址描述 myGe ...
- Unity 3D UGUI Toggle用法教程
UGUI Toggle用法教程 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- textarea(多行文本域)
多行文本域<textarea>: <textarea name="..." rows="..." cols="..." . ...
- 论文阅读理解 - Stacked Hourglass Networks for Human Pose Estimation
http://blog.csdn.net/zziahgf/article/details/72732220 keywords 人体姿态估计 Human Pose Estimation 给定单张RGB图 ...