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)的更多相关文章

  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. ubuntu16.04系统安装

    0x1镜像下载 (1)下载地址http://cn.ubuntu.com/download/ 0x2 安装 (1)打开vmware,创建新的虚拟机 (2)选择自定义安装 (3)直接下一步,选择稍后安装系 ...

  2. 微软Power BI 每月功能更新系列——5月Power BI 新功能学习

    Power BI Desktop 5月份功能摘要 本月Power BI Desktop除了许多报表功能的更新,Power BI对条件格式进行了重大改进,可以对报表的任何字段(包括字符串和日期)进行条件 ...

  3. Python 使用sqlalchemy操作MYSQL

    1. 安装sqlalchemy库 SQL操作引擎可能需要pymysql,故要安装如下两个程序 pip install sqlalchemy pip instal pymysql

  4. [转]一文读懂《梁宁·产品思维30讲》最精华内容(含全套PPT)

    http://chuansong.me/n/2294260949029 8 年前,我的主业是产品经理,产品思维改变了我认识世界的方式,让我明白司空见惯的设计,也有其底层逻辑. 几年后我接触培训.运营自 ...

  5. [转]Cross-type joins in Elasticsearch

    Cross-type joins in Elasticsearch http://rore.im/posts/elasticsearch-joins December 31, 2014 When mo ...

  6. java ip number to string

    package com.awkj; import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownH ...

  7. Linux不在显示器上方总是显示企鹅

    /********************************************************************************* * Linux不在显示器上方总是显 ...

  8. 【linux基础】使用命令行编译运行c++程序

    前言 在linux系统运行程序,小鹅知道的有3种编译方式,一种是直接命令行编译,一种是使用Cmake,一种是使用脚本文件(*.sh).本文介绍的是使用命令行编译. 使用过程 注意不同系统的编译器版本可 ...

  9. Skflow mac安装 for tensorflow-0.8.0

    参考: # Mac OS X, CPU only: $ pip install --ignore-installed --upgrade https://storage.googleapis.com/ ...

  10. JavaBasic_04

    选择结构 选择结构有特定的语法规则 代码要执行具体的逻辑运算进行判断(代码执行有条件) 逻辑运算的结果有两个,所以产生选择,按照不同的选择执行不同的代码(根据不同的条件执行不同的代码) Java语言提 ...