1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python字符串去除(中文 英文 数字 标点符号)

python字符串去除(中文 英文 数字 标点符号)

时间:2019-07-02 20:40:30

相关推荐

python字符串去除(中文 英文 数字 标点符号)

/weixin_43360896/article/details/114499028

python | 字符串去除(中文、英文、数字、标点符号)

去除标点符号

去除英文标点符号

去除中文标点符号

去除中文

去除英文

去除数字

去除空格

介绍下去除各个字母、数字、符号的方法,主要就是re的运用,去除用re.sub(),而反过来的提取用re.findall()即可

去除标点符号

标点符号包括中英文两种,要分开处理

去除英文标点符号

string.punctuation包含所有英文标点符号

import string

string.punctuation

1

2

'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

1

text = '''Don't worry, be happy!''' # 'Don\'t worry, be happy'

punctuation_string = string.punctuation

for i in punctuation_string:

text = text.replace(i, '')

print(text)

1

2

3

4

5

6

Dont worry be happy

1

import re

re.sub('[{}]'.format(punctuation_string),"",text)

1

2

'Dont worry be happy'

1

去除中文标点符号

调用zhon包的zhon.hanzi.punctuation函数即可得到中文的标点符号集合。

from zhon.hanzi import punctuation

punctuation

1

2

'"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、\u3000、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·!?。。'

1

text = '生活就像【】,如果##'

punctuation_str = punctuation

for i in punctuation_str:

text = text.replace(i, '')

text

1

2

3

4

5

6

7

'生活就像如果'

1

import re

re.sub('[{}]'.format(punctuation),"",text)

1

2

'生活就像如果'

1

去除中文

import re

text = '生活就像【】,如果##'

temp = re.sub('[\u4e00-\u9fa5]','',text)

temp

1

2

3

4

5

6

'【】,##'

1

from zhon.hanzi import characters

import re

text = '生活就像【】,如果##'

temp = re.sub('[{}]'.format(characters),'',text)

temp

1

2

3

4

5

6

7

'【】,##'

1

去除英文

import re

text="aksjn ekljfk # ! len223"

temp = re.sub('[a-zA-Z]','',text)

temp

1

2

3

4

5

6

' # ! 223'

1

去除数字

其实对于\d \s \w这些,小写是数字\空格\数字字母,大写即是非数字\非空格\非数字字母,可以合理运用~

import re

text="哈aksjn ekljfk # ! len223"

temp = re.sub('[\d]','',text) # [0-9]

temp

1

2

3

4

5

6

'哈aksjn ekljfk # ! len'

1

去除空格

有很多方法,比如:

Python 字符串去除空格的方法

import re

text="aksjn ekljfk # ! len223"

temp = re.sub('[\s]','',text) #temp = text.strip()

temp

1

2

3

4

5

6

'aksjn ekljfk # ! len223'

————————————————

版权声明:本文为CSDN博主「买猫咪的小鱼干」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:/weixin_43360896/article/details/114499028

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。