unicodedata.normalize()清理字符串

# normalize()的第一个参数指定字符串标准化的方式,分别有NFD/NFC

  1. >>> s1 = 'Spicy Jalape\u00f1o'
  2. >>> s2 = 'Spicy Jalapen\u0303o'
  3. >>> import unicodedata
  4. # NFC表示字符应该是整体组成(可能是使用单一编码)
  5. >>> t1 = unicodedata.normalize('NFC', s1)
  6. >>> t2 = unicodedata.normalize('NFC', s2)
  7. >>> t1 == t2
  8. True
  9. # NFD表示字符应该分解为多个组合字符表示
  10. >>> t1 = unicodedata.normalize('NFD', s1)
  11. >>> t2 = unicodedata.normalize('NFD', s2)
  12. >>> t1 == t2
  13. True

注:Python中同样支持NFKC/NFKD,使用原理同上

combining()匹配文本上的和音字符

  1. >>> s1
  2. 'Spicy Jalapeño'
  3. >>> t1 = unicodedata.normalize('NFD', s1)
  4. >>> ''.join(c for c in t1 if not unicodedata.combining(c)) # 去除和音字符
  5. 'Spicy Jalapeno'

使用strip()、rstrip()和lstrip()

  1. >>> s = ' hello world \n'
  2. # 去除左右空白字符
  3. >>> s.strip()
  4. 'hello world'
  5. # 去除右边空白字符
  6. >>> s.rstrip()
  7. ' hello world'
  8. # 去除左边空白字符
  9. >>> s.lstrip()
  10. 'hello world \n'
  11. >>> t = '-----hello====='
  12. # 去除左边指定字段('-')
  13. >>> t.lstrip('-')
  14. 'hello====='
  15. # 去除右边指定字段('-')
  16. >>> t.rstrip('=')
  17. '-----hello'

# 值得注意的是,strip等不能够去除中间空白字符,要使用去除中间空白字符可以使用下面方法

  1. >>> s = ' hello world \n'
  2. # 使用replace()那么会造成"一个不留"
  3. >>> s.replace(' ', '')
  4. 'helloworld\n'
  5. # 使用正则
  6. >>> import re
  7. >>> re.sub(r'\s+', ' ', s)
  8. ' hello world '

关于translate()

# 处理和音字符

  1. >>> s = 'pýtĥöñ\fis\tawesome\r\n'
  2. >>> remap = {ord('\r'): None, ord('\t'): ' ', ord('\f'): ' '} # 构造字典,对应空字符
  3. >>> a = s.translate(remap) # 进行字典转换
  4. >>> a
  5. 'pýtĥöñ is awesome\n'
  6. >>> import unicodedata
  7. >>> import sys
  8. >>> cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode) if unicodedata.combining(chr(c))) # 查找系统的和音字符,并将其设置为字典的键,值设置为空
  9. >>> b = unicodedata.normalize('NFD', a) # 将原始输入标准化为分解形式字符
  10. >>> b
  11. 'pýtĥöñ is awesome\n'
  12. >>> b.translate(cmb_chrs)
  13. 'python is awesome\n'

# 将所有的Unicode数字字符映射到对应的ASCII字符上

  1. # unicodedata.digit(chr(c)) # 将ASCII转换为十进制数字,再加上'0'的ASCII就对应了“0~9”的ASCII码
  2. >>> 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字符
  3. >>> len(digitmap)
  4. 610
  5. >>> x = '\u0661\u0662\u0663'
  6. >>> x.translate(digitmap)
  7. ''

关于I/O解码和编码函数

  1. >>> a
  2. 'pýtĥöñ is awesome\n'
  3. >>> b = unicodedata.normalize('NFD', a)
  4. >>> b.encode('ascii', 'ignore').decode('ascii')
  5. 'python is awesome\n'

unicodedata.normalize()/使用strip()、rstrip()和lstrip()/encode和decode 笔记(具体可看 《Python Cookbook》3rd Edition 2.9~2.11)的更多相关文章

  1. 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...

  2. 探究 encode 和 decode 的使用问题(Python)

    很多时候在写Python程序的时候都要在头部添加这样一行代码 #coding: utf-8 或者是这样 # -*- coding:utf-8 -*- 等等 这行代码的意思就是设定同一编码格式为utf- ...

  3. python的str,unicode对象的encode和decode方法(转)

    python的str,unicode对象的encode和decode方法(转) python的str,unicode对象的encode和decode方法 python中的str对象其实就是" ...

  4. 48-python基础-python3-字符串-常用字符串方法(六)-strip()-rstrip()-lstrip()

    7-用 strip().rstrip()和 lstrip()删除空白字符 strip()字符串方法将返回一个新的字符串,它的开头或末尾都没有空白字符. lstrip()和 rstrip()方法将相应删 ...

  5. [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 ...

  6. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  7. LeetCode Encode and Decode Strings

    原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...

  8. Encode and Decode Strings

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  9. encode和decode

    Python字符串的encode与decode研究心得乱码问题解决方法 为什么会报错“UnicodeEncodeError: 'ascii' codec can't encode characters ...

随机推荐

  1. Python 依赖关系

    class Person: def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来 tools.run() print("很开心, 我能玩儿游戏了&quo ...

  2. python 基础 内置函数 和lambda表达式

    1.把任意数值转化为字符串有两种方法. (1)str()用于将数值转化为易于人读的形式.print(str("我是中国人"))>>>我是中国人 (2)repr() ...

  3. Golang游戏服务器与skynet的个人直观比较

    我对和GOLANG写MMO服务器的一些遐想: 1.沙盒(隔离性) SKYNET:原生LUA STATE作为沙盒, 进行服务器间隔离安全性高: 服务可以很容易的配置到不同节点之上. GO:估计用RECO ...

  4. PHP设计模式之观察者模式(转)

    开篇还是从名字说起,“观察者模式”的观察者三个字信息量很大.玩过很多网络游戏的童鞋们应该知道,即便是斗地主,除了玩家,还有一个角色叫“观察者".在我们今天他谈论的模式设计中,观察者也是如此. ...

  5. 【集成学习】sklearn中xgboost模块的XGBClassifier函数

    # 常规参数 booster gbtree 树模型做为基分类器(默认) gbliner 线性模型做为基分类器 silent silent=0时,不输出中间过程(默认) silent=1时,输出中间过程 ...

  6. 求割点 割边 Tarjan

    附上一般讲得不错的博客 https://blog.csdn.net/lw277232240/article/details/73251092 https://www.cnblogs.com/colle ...

  7. 百度地图api开发:根据坐标获得地理描述地址

    // 创建地理编码实例              var myGeo = new BMap.Geocoder();              // 根据坐标得到地址描述            myGe ...

  8. Unity 3D UGUI Toggle用法教程

    UGUI Toggle用法教程 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...

  9. textarea(多行文本域)

    多行文本域<textarea>: <textarea name="..." rows="..." cols="..." . ...

  10. 论文阅读理解 - Stacked Hourglass Networks for Human Pose Estimation

    http://blog.csdn.net/zziahgf/article/details/72732220 keywords 人体姿态估计 Human Pose Estimation 给定单张RGB图 ...