python去除拼音声调字母,替换为字母的方法
作者:瓜而不皮 时间:2022-02-13 10:43:55
第一种方法
import sys
import unicodedata
s = "Lǐ Zhōu Wú"
remap = {
# ord返回ascii值
ord('\t'): '',
ord('\f'): '',
ord('\r'): None
}
# 去除\t, \f, \r
a = s.translate(remap)
'''
通过使用dict.fromkeys() 方法构造一个字典,每个Unicode 和音符作为键,对于的值全部为None
然后使用unicodedata.normalize() 将原始输入标准化为分解形式字符
sys.maxunicode : 给出最大Unicode代码点的值的整数,即1114111(十六进制的0x10FFFF)。
unicodedata.combining:将分配给字符chr的规范组合类作为整数返回。 如果未定义组合类,则返回0。
'''
cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode) if unicodedata.combining(chr(c))) #此部分建议拆分开来理解
b = unicodedata.normalize('NFD', a)
'''
调用translate 函数删除所有重音符
'''
print(b.translate(cmb_chrs))
第二种方法 (推荐)
import unicodedata
mystr='Lǐ Zhōu Wú'
unicodedata.normalize('NFKD', mystr).encode('ascii','ignore')
来源:https://blog.csdn.net/qq_32590631/article/details/81369980
标签:python,拼音,声调,字母
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用游标进行PHP SQLSRV查询的方法与注意事项
2023-05-22 10:51:10
Apache下禁止特定目录执行PHP 提高服务器安全性
2023-10-25 20:10:50
MySQL数据库的自动备份与数据库被破坏后的恢复
2010-03-18 15:30:00
Python快速生成定制化的Word(docx)文档
2022-04-27 12:33:06
![](https://img.aspxhome.com/file/2023/8/75298_0s.png)
javascript中css的float特殊写法
2007-12-24 20:24:00
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
2023-05-19 13:05:18
![](https://img.aspxhome.com/file/2023/9/90709_0s.jpg)
网站中美好的细节
2011-07-13 18:43:07
JSP 获取spring容器中bean的两种方法总结
2023-06-15 07:27:46
python-OpenCV 实现将数组转换成灰度图和彩图
2023-07-22 11:22:19
![](https://img.aspxhome.com/file/2023/9/86649_0s.jpg)
python光学仿真面向对象光学元件类的实现
2022-10-11 19:45:06
经典的退出浏览器弹窗代码
2008-07-30 12:48:00
ASP 根据用户权限判断显示的列标题
2011-03-29 11:01:00
在MySQL中使用XML数据—数据格式化
2009-12-29 10:26:00
python2.7删除文件夹和删除文件代码实例
2023-06-02 14:41:17
Python常用模块介绍
2021-01-03 10:11:53
Bootstrap实现渐变顶部固定自适应导航栏
2023-08-23 00:52:40
![](https://img.aspxhome.com/file/2023/8/56078_0s.png)
pycharm无法导入lxml的解决办法
2023-08-24 04:34:39
![](https://img.aspxhome.com/file/2023/2/87922_0s.png)
Python中logging日志模块代码调试过程详解
2021-03-13 05:40:32
DIV+CSS常见错误及解决方法
2008-04-25 22:46:00
Python 使用tf-idf算法计算文档关键字权重并生成词云的方法
2022-03-28 13:09:18
![](https://img.aspxhome.com/file/2023/5/82965_0s.png)