python中如何去除标点符号


本文摘自php中文网,作者尚,侵删。

Python去掉标点符号的方法如下:

方法一:

str.isalnum:

S.isalnum() -> bool

返回值:如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False。

实例:

1

2

3

>>> string = "Special $#! characters   spaces 888323"

>>> ''.join(e for e in string if e.isalnum())

'Specialcharactersspaces888323'

只能识别字母和数字,杀伤力大,会把中文、空格之类的也干掉

方法二:

string.punctuation

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

import re, string

 

s ="string. With. Punctuation?" # Sample string

 

# 写法一:

out = s.translate(string.maketrans("",""), string.punctuation)

 

# 写法二:

out = s.translate(None, string.punctuation)

 

# 写法三:

exclude = set(string.punctuation)

out = ''.join(ch for ch in s if ch not in exclude)

 

# 写法四:

>>> for c in string.punctuation:

            s = s.replace(c,"")

>>> s

'string With Punctuation'

 

# 写法五:

out = re.sub('[%s]' % re.escape(string.punctuation), '', s)

## re.escape:对字符串中所有可能被解释为正则运算符的字符进行转义

 

# 写法六:

# string.punctuation 只包括 ascii 格式; 想要一个包含更广(但是更慢)的方法是使用: unicodedata module :

from unicodedata import category

s = u'String — with - ?Punctuation ?...'

out = re.sub('[%s]' % re.escape(string.punctuation), '', s)

print 'Stripped', out

# 输出:u'Stripped String \u2014 with  \xabPunctuation \xbb'

out = ''.join(ch for ch in s if category(ch)[0] != 'P')

print 'Stripped', out

# 输出:u'Stripped String  with  Punctuation '

 

 

# For Python 3 str or Python 2 unicode values, str.translate() only takes a dictionary; codepoints (integers) are looked up in that mapping and anything mapped to None is removed.

# To remove (some?) punctuation then, use:

import string

remove_punct_map = dict.fromkeys(map(ord, string.punctuation))

s.translate(remove_punct_map)

 

 

# Your method doesn't work in Python 3, as the translate method doesn't accept the second argument any more.

import unicodedata

import sys

tbl = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P'))

def remove_punctuation(text):

    return text.translate(tbl)

方法三:

阅读剩余部分

相关阅读 >>

Python怎么爬取数据

Python3中获取文件当前绝对路径的两种方法

Python是强类型语言吗

numpy中实现ndarray数组返回符合特定条件的索引方法

Python整数怎么表示

Python如何实现客户端和服务器端的数据传输(代码)

Python求两个数的最大公约数

Python3实现发送qq邮件功能(文本)_Python

Python怎么写有死循环的程序

Python读写excel文档

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...