python3小技巧之:妙用string.punctuation
前言
在对字符串操作时,如果感觉自己写的很复杂时,可以试试string模块,里面有很多实用的属性。
>>> import string
>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__built
ins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__packag
e__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_u
ppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctua
tion', 'whitespace']
>>> string.ascii_lowercase #所有的小写字母
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase #所有的大写字母
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.hexdigits #所有的十六进制字符
'0123456789abcdefABCDEF'
>>> string.whitespace #所有的空白字符
' \t\n\r\x0b\x0c'
>>> string.punctuation #所有的标点字符
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
问题
统计一个文件或一个字符串中所有单词出现的次数。由于句子中存在标点符号,直接对字符串切割的话会把单词和标点切割在一起,比如:
如果指定标点符号切割的话,当句子很长或者里面有很多种标点的话操作起来相当的麻烦。
解决方法
思路:先把句子中的标点符号统一替换为空格,然后在split()切割即可搞定。这时候就可以用上string.punctuation
上代码:
import string #注意使用前要先将string模块导入>>> s="We met at the wrong time, but separated at the right time. The most urgen
t is to take the most beautiful scenery!!! the deepest wound was the most real e
motions."
>>> for i in s:
... if i in string.punctuation: #如果字符是标点符号的话就将其替换为空格
... s = s.replace(i," ")
...
>>> s
'We met at the wrong time but separated at the right time The most urgent is t
o take the most beautiful scenery the deepest wound was the most real emotion
s '
>>> s.split()#按空白切割
['We', 'met', 'at', 'the', 'wrong', 'time', 'but', 'separated', 'at', 'the', 'ri
ght', 'time', 'The', 'most', 'urgent', 'is', 'to', 'take', 'the', 'most', 'beaut
iful', 'scenery', 'the', 'deepest', 'wound', 'was', 'the', 'most', 'real', 'emot
ions']
>>>
当然这个问题也可以用正则解决:
>>> import re
>>> s="We met at the wrong time, but separated at the right time. The most urgen
t is to take the most beautiful scenery!!! the deepest wound was the most real e
motions."
>>> re.findall(r'\b\w+\b',s)
['We', 'met', 'at', 'the', 'wrong', 'time', 'but', 'separated', 'at', 'the', 'ri
ght', 'time', 'The', 'most', 'urgent', 'is', 'to', 'take', 'the', 'most', 'beaut
iful', 'scenery', 'the', 'deepest', 'wound', 'was', 'the', 'most', 'real', 'emot
ions']
解决一个问题的方法有很多种,可以多尝试几种,锻炼自己的思维。在字符串操作的时候,如果感觉写的很麻烦的话,一定记得string模块,看是否能更加简单的解决问题。
python3小技巧之:妙用string.punctuation相关推荐
- Python3 小技巧三 每十个数取一次值
题:元组有99个元素,我要每10个取一次 获得[xx,xx,xx,xx,...] 答案形式:[1-10] [11-20] [21-30] .... a = [1,2,3,4,5,6,7,8,9,1 ...
- Java中String对象的replaceAll方法调用性能优化小技巧
Java中String对象的replaceAll方法调用性能优化小技巧 0x01 Java中String对象的replaceAll方法调用性能优化小技巧 1.1 What? 1.2 Why? 1.3 ...
- python小技巧,通过excle生成字典型变量
python3 小技巧, 通过Excel数据生成字典类型变量 本技巧需要用到Excel.txet文档参与 基本思路:在Excel中写入数据,一列为字典的key,另一列为字典的value,然后将Exce ...
- Delphi三层开发小技巧:TClientDataSet的Delta妙用
Delphi三层开发小技巧:TClientDataSet的Delta妙用 Delphi做三层开发时,很多人都会在客户端放一个TClientDataSet,中间层远程数据模块就对应放一个TDataSet ...
- 输入5个整形数据_妙招技法:Excel表格数据录入的5个小技巧
在Excel表格中录入数据,看似很简单,但是有一些实用小技巧,你不一定知道哦! 1 技巧1:快速插入特殊符号 在制作Excel表格时,有些表格需要输入一些符号,键盘上有的符号(如@.#.¥.%.$.^ ...
- Matplotlib使用小技巧(Python3.5版)
Matplotlib使用小技巧(Python3.5版) 参考链接https://mp.weixin.qq.com/s/aMPH4ABeNlRJ5kBWE-vGPg 1.中文显示 Matplotlib默 ...
- Python小技巧:富比较方法的妙用,__lt__、__le__、__eq__、__ne__、__gt__、__ge__。。。
前言 这里是Python小技巧的系列文章.这是第二篇,富比较方法的妙用. 在 Python中,富比较方法共6个,如下表所示: 见名知意,富比较主要用于比较. 富比较方法 使用 释义 释义 object ...
- 华为手机备忘录的妙用——8个小技巧总结
现在很多人出门必带的是什么东西?没错,就是你脑海里闪过的词--手机.现在人们越来越离不开手机,但是说到华为手机备忘录的妙用你知道吗?大多数人是回答不全的.接下来,小编将会总结关于华为手机备忘录的8个妙 ...
- Golang 中使用 JSON 的一些小技巧 陶文 陶文 3 个月前 有的时候上游传过来的字段是string类型的,但是我们却想用变成数字来使用。 本来用一个json:,string 就可以支持了
Golang 中使用 JSON 的一些小技巧 陶文 3 个月前 有的时候上游传过来的字段是string类型的,但是我们却想用变成数字来使用. 本来用一个json:",string" ...
最新文章
- python【蓝桥杯vip练习题库】ADV-183分苹果(差分数组 离线区间)
- Struts2的文件目录
- 逆序输出1到4位正整数
- 当社恐和社恐相亲时,场面会有多尴尬?
- kgdb调试内核无法执行断点及kdb-22:Permisson denied
- C语言:学生信息管理程序
- scrapy Crawl_spider
- 不定宽高的DIV,垂直水平居中
- 大数据分析有哪些分析模型
- android ndk makefile,用Android NDK打造自己的toolchain(使用C/C++默认的Makefile)
- 用Python实现一个动物识别专家系统--人工智能
- 支持hicar的华为手机
- ros pppoe服务器性能,ROS软路由中最详细的PPPOE服务器设置教程
- debian 5常用软件包名称,及安装方法
- 安装XAMPP遇到的问题(一)——要求关闭UAC以及权限问题
- UVM 事务级建模TLM 单向/多向通信 端口 FIFO通信
- 从零到一写一个完整的 Compose 版本的天气
- Flume跨服务器采集数据
- 【论文晨读】一种移动机器人全局最优路径规划算法 孟 偲2008
- 计算机教学与实践,计算机教学研究与实践——2010学术年会论文集