从Python中删除字符串标点符号的最佳方法

Law*_*ton 578 python string punctuation

似乎应该有一个比以下更简单的方法:

import string
s = "string. With. Punctuation?" # Sample string 
out = s.translate(string.maketrans("",""), string.punctuation)
Run Code Online (Sandbox Code Playgroud)

在那儿?

Bri*_*ian 828

从效率的角度来看,你不会打败

s.translate(None, string.punctuation)
Run Code Online (Sandbox Code Playgroud)

它使用查找表在C中执行原始字符串操作 - 除了编写自己的C代码之外,没有什么能比这更好.

如果速度不是担心,另一个选择是:

s.translate(str.maketrans('', '', string.punctuation))
Run Code Online (Sandbox Code Playgroud)

这比使用每个char的s.replace更快,但是不能像非纯python方法那样执行,例如regexes或string.translate,正如您可以从下面的时间看到的那样.对于这种类型的问题,尽可能低的水平做到这一点是值得的.

时间码:

exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)
Run Code Online (Sandbox Code Playgroud)

这给出了以下结果:

import re, string, timeit

s = "string. With. Punctuation"
exclude = set(string.punctuation)
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))

def test_set(s):
    return ''.join(ch for ch in s if ch not in exclude)

def test_re(s):  # From Vinko's solution, with fix.
    return regex.sub('', s)

def test_trans(s):
    return s.translate(table, string.punctuation)

def test_repl(s):  # From S.Lott's solution
    for c in string.punctuation:
        s=s.replace(c,"")
    return s

print "sets      :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000)
print "regex     :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000)
print "translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000)
print "replace   :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000)
Run Code Online (Sandbox Code Playgroud)

  • 在Python3中,`table = string.maketrans("","")`应该替换为`table = str.maketrans({key:None for string in string.punctuation})`? (33认同)
  • 很好的答案.您可以通过删除表来简化它.文档说:"对于只删除字符的翻译,将表格参数设置为无"(http://docs.python.org/library/stdtypes.html#str.translate) (29认同)
  • 感谢时间信息,我正在考虑自己做类似的事情,但你的写作比我想做的更好,现在我可以用它作为我想写的任何未来时序代码的模板:). (25认同)
  • 要更新讨论,从Python 3.6开始,`regex`现在是最有效的方法!它几乎比翻译快2倍.而且,设置和替换不再那么糟糕!他们都提高了4倍:) (15认同)
  • 值得注意的是,translate()对str和unicode对象的行为有所不同,所以你需要确保你总是使用相同的数据类型,但这个答案中的方法同样适用于两者,这很方便. (3认同)
  • @mlissner - 效率.它是一个列表/字符串,您需要进行线性扫描以确定字母是否在字符串中.但是,使用集合或字典,它通常会更快(除了非常小的字符串),因为它不必检查每个值. (2认同)
  • 在Python 3中,转换表也可以通过`table = str.maketrans('',``,string.punctuation)`https://docs.python.org/3/library/stdtypes.html#str创建。 maketrans (2认同)
  • >>> s.translate(None, string.punctuation) Traceback (最近一次调用最后): File "<stdin>", line 1, in <module> TypeError: str.translate() 仅接受一个参数(给定 2 个参数)在python3中,我认为这个答案需要更新 (2认同)

Era*_*nes 123

正则表达式很简单,如果你知道的话.

import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我们用空字符串替换(re.sub)所有NON [字母数字字符(\ w)和空格(\ s)].
因此.和?通过正则表达式运行s变量后,变量's'中不会出现标点符号.

  • @Outlier说明:用空字符串替换不是(^)字符或空格.但要小心,\ w通常也会匹配下划线. (3认同)
  • @SIslam我认为它将与unicode一起使用unicode标志设置,即`s = re.sub(r'[^\w\s]','',s,re.UNICODE)`.在linux上使用python 3进行测试即使没有使用tamil字母,தமிழ்的标志也能正常工作. (2认同)

Spa*_*ine 65

为了方便使用,我总结了Python 2和Python 3中字符串条带标点符号的注释.请参阅其他答案以获取详细说明.


Python 2

import string

s = "string. With. Punctuation?"
table = string.maketrans("","")
new_s = s.translate(table, string.punctuation)      # Output: string without punctuation
Run Code Online (Sandbox Code Playgroud)

Python 3

import string

s = "string. With. Punctuation?"
table = str.maketrans(dict.fromkeys(string.punctuation))  # OR {key: None for key in string.punctuation}
new_s = s.translate(table)                          # Output: string without punctuation
Run Code Online (Sandbox Code Playgroud)


小智 51

myString.translate(None, string.punctuation)
Run Code Online (Sandbox Code Playgroud)

  • `TypeError:translate()只取一个参数(给定2个)`:( (41认同)
  • 请注意,对于Python 3中的`str`和Python 2中的`unicode`,不支持`deletechars`参数. (12认同)
  • 啊,我试过这个,但它并不适用于所有情况.myString.translate(string.maketrans("",""),string.punctuation)工作正常. (4认同)
  • myString.translate(string.maketrans("",""),string.punctuation)不适用于unicode字符串(找出困难的方法) (4认同)
  • @BrianTingle:在我的评论中查看Python 3代码(它传递一个参数).[按照链接,查看适用于unicode的Python 2代码](http://stackoverflow.com/a/11066687/4279)和[它的Python 3改编](http://stackoverflow.com/a/21635971/ 4279) (3认同)
  • @agf:你仍然可以[使用`.translate()`删除标点符号,即使在Unicode和py3k情况下也是如此](http://stackoverflow.com/a/11066687/4279)使用字典参数. (2认同)
  • @MarcMaxson:myString.translate(str.maketrans(“”,“”,string.punctuation))确实适用于Python 3上的Unicode字符串。尽管string.punctuation`仅包含ASCII标点。单击[我以前的评论中的链接](http://stackoverflow.com/a/11066687/4279)。它显示了如何删除所有标点符号(包括Unicode标点符号)。 (2认同)

S.L*_*ott 27

我经常使用这样的东西:

>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
...     s= s.replace(c,"")
...
>>> s
'string With Punctuation'
Run Code Online (Sandbox Code Playgroud)

  • uglified one-liner:`reduce(lambda s,c:s.replace(c,''),string.punctuation,s)`. (2认同)

Bjö*_*ist 24

string.punctuation是ASCII !更正确(但也更慢)的方法是使用unicodedata模块:

# -*- coding: utf-8 -*-
from unicodedata import category
s = u'String — with -  «punctation »...'
s = ''.join(ch for ch in s if category(ch)[0] != 'P')
print 'stripped', s
Run Code Online (Sandbox Code Playgroud)

  • 你可以:[`regex.sub(ur"\ p {P} +","",text)`](http://stackoverflow.com/a/11066687/4279) (3认同)

Vin*_*vic 21

如果你对家庭更熟悉,不一定更简单,但不一样.

import re, string
s = "string. With. Punctuation?" # Sample string 
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
Run Code Online (Sandbox Code Playgroud)

  • 实际上,它还是错的.序列"\\]"被视为一个转义(巧合地没有关闭],因此绕过了另一个失败),但是离开了\ unescaped.您应该使用re.escape(string.punctuation)来防止这种情况. (2认同)

Mar*_*ers 12

对于Python 3 str或Python 2 unicode值,str.translate()只需要一个字典; 在该映射中查找代码点(整数),并None删除映射到的任何内容.

要删除(某些?)标点符号,请使用:

import string

remove_punct_map = dict.fromkeys(map(ord, string.punctuation))
s.translate(remove_punct_map)
Run Code Online (Sandbox Code Playgroud)

所述dict.fromkeys()类方法使得它琐碎创建映射,所有的值设置为None基于密钥的序列.

要删除所有标点符号,而不仅仅是ASCII标点符号,您的表格需要更大一些; 请参阅JF Sebastian的回答(Python 3版本):

import unicodedata
import sys

remove_punct_map = dict.fromkeys(i for i in range(sys.maxunicode)
                                 if unicodedata.category(chr(i)).startswith('P'))
Run Code Online (Sandbox Code Playgroud)


Zac*_*ach 12

string.punctuation错过了现实世界中常用的标点符号.如何使用适用于非ASCII标点符号的解决方案?

import regex
s = u"string. With. Some?Really Weird?Non?ASCII? ??Punctuation???"
remove = regex.compile(ur'[\p{C}|\p{M}|\p{P}|\p{S}|\p{Z}]+', regex.UNICODE)
remove.sub(u" ", s).strip()
Run Code Online (Sandbox Code Playgroud)

就个人而言,我认为这是从Python中删除字符串标点符号的最佳方法,因为:

  • 它删除所有Unicode标点符号
  • 它很容易修改,例如你可以删除\{S}如果你想删除标点,但保持符号$.
  • 您可以非常具体地了解要保留的内容以及要删除的内容,例如,\{Pd}只删除短划线.
  • 这个正则表达式也规范了空白.它将标签,回车和其他奇怪的地方映射到漂亮的单个空间.

这使用Unicode字符属性,您可以在维基百科上阅读更多信息.


Tim*_*m P 8

这是Python 3.5的单行程序:

import string
"l*ots! o(f. p@u)n[c}t]u[a'ti\"on#$^?/".translate(str.maketrans({a:None for a in string.punctuation}))
Run Code Online (Sandbox Code Playgroud)


Bla*_*g23 8

我还没有看到这个答案.只需使用正则表达式; 它除了单词字符(\w)和数字字符(\d)之外的所有字符,后跟一个空白字符(\s):

import re
s = "string. With. Punctuation?" # Sample string 
out = re.sub(ur'[^\w\d\s]+', '', s)
Run Code Online (Sandbox Code Playgroud)

  • `\d` 是多余的,因为它是 `\w` 的子集。 (2认同)

小智 6

这可能不是最好的解决方案,但这就是我做到的.

import string
f = lambda x: ''.join([i for i in x if i not in string.punctuation])
Run Code Online (Sandbox Code Playgroud)


Dr.*_*ogy 6

这是我写的一个函数.它不是很有效,但它很简单,您可以添加或删除任何您想要的标点符号:

def stripPunc(wordList):
    """Strips punctuation from list of words"""
    puncList = [".",";",":","!","?","/","\\",",","#","@","$","&",")","(","\""]
    for punc in puncList:
        for word in wordList:
            wordList=[word.replace(punc,'') for word in wordList]
    return wordList
Run Code Online (Sandbox Code Playgroud)


小智 5

在不太严格的情况下,单行可能会有所帮助:

''.join([c for c in s if c.isalnum() or c.isspace()])
Run Code Online (Sandbox Code Playgroud)


小智 5

>>> s = "string. With. Punctuation?"
>>> s = re.sub(r'[^\w\s]','',s)
>>> re.split(r'\s*', s)


['string', 'With', 'Punctuation']
Run Code Online (Sandbox Code Playgroud)

  • 请使用更多信息进行编辑。不鼓励只使用代码和“试试这个”的答案,因为它们不包含可搜索的内容,并且没有解释为什么有人应该“试试这个”。 (2认同)

ngu*_*b05 5

这是一个没有正则表达式的解决方案。

import string

input_text = "!where??and!!or$$then:)"
punctuation_replacer = string.maketrans(string.punctuation, ' '*len(string.punctuation))    
print ' '.join(input_text.translate(punctuation_replacer).split()).strip()

Output>> where and or then
Run Code Online (Sandbox Code Playgroud)
  • 用空格替换标点符号
  • 用一个空格替换单词之间的多个空格
  • 使用 strip() 删除尾随空格(如果有)


Hay*_*HAB 5

import re
s = "string. With. Punctuation?" # Sample string 
out = re.sub(r'[^a-zA-Z0-9\s]', '', s)
Run Code Online (Sandbox Code Playgroud)


kri*_*ker 5

作为更新,我重写了Python 3中的@Brian示例,并对其进行了更改,以将regex编译步骤移至函数内部。我的想法是计时使该功能起作用所需的每个步骤。也许您使用的是分布式计算,并且您的工作人员之间无法共享正则表达式对象,因此需要re.compile在每个工作人员中走一步。另外,我很好奇地为Python 3的maketrans的两种不同实现计时了

table = str.maketrans({key: None for key in string.punctuation})
Run Code Online (Sandbox Code Playgroud)

table = str.maketrans('', '', string.punctuation)
Run Code Online (Sandbox Code Playgroud)

另外,我添加了另一种使用set的方法,其中利用了交集函数来减少迭代次数。

这是完整的代码:

import re, string, timeit

s = "string. With. Punctuation"


def test_set(s):
    exclude = set(string.punctuation)
    return ''.join(ch for ch in s if ch not in exclude)


def test_set2(s):
    _punctuation = set(string.punctuation)
    for punct in set(s).intersection(_punctuation):
        s = s.replace(punct, ' ')
    return ' '.join(s.split())


def test_re(s):  # From Vinko's solution, with fix.
    regex = re.compile('[%s]' % re.escape(string.punctuation))
    return regex.sub('', s)


def test_trans(s):
    table = str.maketrans({key: None for key in string.punctuation})
    return s.translate(table)


def test_trans2(s):
    table = str.maketrans('', '', string.punctuation)
    return(s.translate(table))


def test_repl(s):  # From S.Lott's solution
    for c in string.punctuation:
        s=s.replace(c,"")
    return s


print("sets      :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000))
print("sets2      :",timeit.Timer('f(s)', 'from __main__ import s,test_set2 as f').timeit(1000000))
print("regex     :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000))
print("translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000))
print("translate2 :",timeit.Timer('f(s)', 'from __main__ import s,test_trans2 as f').timeit(1000000))
print("replace   :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000))
Run Code Online (Sandbox Code Playgroud)

这是我的结果:

sets      : 3.1830138750374317
sets2      : 2.189873124472797
regex     : 7.142953420989215
translate : 4.243278483860195
translate2 : 2.427158243022859
replace   : 4.579746678471565
Run Code Online (Sandbox Code Playgroud)


Deh*_* Li 5

为什么你们没人用这个?

 ''.join(filter(str.isalnum, s)) 
Run Code Online (Sandbox Code Playgroud)

太慢了?

  • 请注意,这也会删除空格。 (4认同)

alo*_*oha 5

我一直在寻找一个非常简单的解决方案。这是我得到的:

import re 

s = "string. With. Punctuation?" 
s = re.sub(r'[\W\s]', ' ', s)

print(s)
'string  With  Punctuation '
Run Code Online (Sandbox Code Playgroud)