python 去掉标点符号

Python09

python 去掉标点符号,第1张

这个明显是错误的,你根本没理解replace函数是怎么用的。

Python str.replace(old, new[, max])

方法把字符串str中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max

次。

如果非要用replace()函数来实现要这样写:

import string

m = l

for c in string.punctuation:  

    m = m.replace(c,")

更简便的方法是用translate(),代码如下:

import string

m = l.translate(None, string.punctuation)

用下面这串代码即可去掉标点符号

import string

m = l.translate(None, string.punctuation)

Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,

用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。

import unicodedata

import sys

tbl = dict.fromkeys(i for i in xrange(sys.maxunicode)

if unicodedata.category(unichr(i)).startswith('P'))

def remove_punctuation(text):

return text.translate(tbl)

import regex as re

def remove_punctuation(text):

return re.sub(ur"\p{P}+", "", text)