【问题标题】:Best way to strip punctuation from a string从字符串中去除标点符号的最佳方法
【发布时间】:2010-09-20 22:13:24
【问题描述】:

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

import string
s = "string. With. Punctuation?" # Sample string 
out = s.translate(string.maketrans("",""), string.punctuation)

有吗?

【问题讨论】:

  • 对我来说似乎很简单。你为什么要改变它?如果你想让它更简单,只需将你刚刚编写的内容包装在一个函数中。
  • 嗯,使用 str.translate 的一种副作用来完成这项工作似乎有点骇人听闻。我在想可能有更像 str.strip(chars) 的东西,它适用于整个字符串,而不仅仅是我错过的边界。
  • 也取决于数据。在服务器名称中使用下划线作为名称的一部分(在某些地方很常见)的数据上使用它可能会很糟糕。只要确保您了解数据及其包含的内容,否则您最终可能会遇到 clbuttic 问题的一个子集。
  • 还取决于您所说的标点符号。 “The temperature in the O'Reilly & Arbuthnot-Smythe server's main rack is 40.5 degrees.”只包含一个标点符号,第二个“.”
  • 我很惊讶没有人提到string.punctuation 根本不包含非英语标点符号。我在想。,!?:ד”〟等等。

标签: python string punctuation


【解决方案1】:

不一定更简单,但如果你对 re 家族更熟悉的话,可以换一种方式。

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

【讨论】:

  • 有效,因为 string.punctuation 具有序列,-。以正确的、升序的、无间隙的、ASCII 顺序。虽然 Python 有这个权利,但当您尝试使用 string.punctuation 的子集时,它可能会因为意外的“-”而成为阻碍。
  • 其实还是错了。序列 "\]" 被视为转义(巧合的是没有关闭 ] 从而绕过另一个故障),但留下 \ 未转义。您应该使用 re.escape(string.punctuation) 来防止这种情况。
  • 是的,我省略了它,因为它适用于示例以保持简单,但你是对的,它应该被合并。
【解决方案2】:

我通常使用这样的东西:

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

【讨论】:

  • 丑化的单线:reduce(lambda s,c: s.replace(c, ''), string.punctuation, s).
  • 很好,但是不会删除一些波动,比如更长的连字符
【解决方案3】:

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

s.translate(None, string.punctuation)

对于更高版本的 Python,请使用以下代码:

s.translate(str.maketrans('', '', string.punctuation))

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

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

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

这比使用每个字符的 s.replace 更快,但性能不如正则表达式或 string.translate 等非纯 python 方法,正如您从下面的时序中看到的那样。对于这种类型的问题,尽可能地降低水平是有回报的。

计时码:

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)

这给出了以下结果:

sets      : 19.8566138744
regex     : 6.86155414581
translate : 2.12455511093
replace   : 28.4436721802

【讨论】:

  • 感谢您的时间信息,我正在考虑自己做类似的事情,但你的写得比我所做的任何事情都要好,现在我可以将它用作我未来任何时间代码的模板想写:)。
  • 很好的答案。您可以通过删除表来简化它。文档说:“对于只删除字符的翻译,将 table 参数设置为 None”(docs.python.org/library/stdtypes.html#str.translate)
  • 值得注意的是 translate() 对于 str 和 unicode 对象的行为不同,因此您需要确保始终使用相同的数据类型,但此答案中的方法同样适用于两者,很方便。
  • 在 Python3 中,table = string.maketrans("","") 应该替换为 table = str.maketrans({key: None for key in string.punctuation})?
  • 更新讨论,从 Python 3.6 开始,regex 现在是最有效的方法!它几乎比翻译快 2 倍。此外,设置和替换不再那么糟糕!它们都提高了 4 倍以上 :)
【解决方案4】:
myString.translate(None, string.punctuation)

【讨论】:

  • 啊,我试过了,但不是在所有情况下都有效。 myString.translate(string.maketrans("",""), string.punctuation) 工作正常。
  • 请注意,对于 Python 3 中的 str 和 Python 2 中的 unicode,不支持 deletechars 参数。
  • myString.translate(string.maketrans("",""), string.punctuation) 不适用于 unicode 字符串(很难找到)
  • TypeError: translate() takes exactly one argument (2 given) :(
  • @BrianTingle:查看我评论中的 Python 3 代码(它传递了一个参数)。 Follow the link, to see Python 2 code that works with unicodeits Python 3 adaptation
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

    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
    

    您也可以概括和剥离其他类型的字符:

    ''.join(ch for ch in s if category(ch)[0] not in 'SP')
    

    它还会删除像 ~*+§$ 这样的字符,这可能是也可能不是“标点符号”,这取决于一个人的观点。

    【讨论】:

    【解决方案7】:

    我喜欢使用这样的函数:

    def scrub(abc):
        while abc[-1] is in list(string.punctuation):
            abc=abc[:-1]
        while abc[0] is in list(string.punctuation):
            abc=abc[1:]
        return abc
    

    【讨论】:

    • 这是从头到尾剥离字符;使用 abc.strip(string.punctuation) 代替。它不会删除中间的这些字符
    • 不管函数是否执行,不必要地使用副作用和在遍历列表时修改列表只会导致灾难。
    【解决方案8】:

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

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

    【讨论】:

    • @Outlier 解释:用空字符串替换非 (^) 单词字符或空格。不过要小心,例如,\w 通常也匹配下划线。
    • @SIslam 我认为它可以与设置了 unicode 标志的 unicode 一起使用,即s = re.sub(r'[^\w\s]','',s, re.UNICODE)。在 Linux 上使用 python 3 对其进行测试,即使没有使用泰米尔语字母 தமிழ் 的标志,它也可以工作。
    • @Matthias 我在 Mac 上尝试了 Python 3.6.5 的代码,泰米尔语字母输出看起来有点不同,输入 தமிழ் 变成 தமழ。我对泰米尔语一无所知,不确定是否符合预期。
    • @Matthias 在处理 UNICODE 孟加拉语文本时会混淆单词边界,无论是否使用 UNICODE 标志都会给出错误的单词。
    【解决方案9】:

    对于 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)
    

    dict.fromkeys() class method 使创建映射变得简单,根据键的顺序将所有值设置为 None

    要删除 all 标点,而不仅仅是 ASCII 标点,您的表格需要大一点;见J.F. Sebastian's answer(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'))
    

    【讨论】:

    • 要支持 Unicode,string.punctuation 是不够的。见my answer
    • @J.F.Sebastian:确实,我的回答只是使用与投票最多的字符相同的字符。为您的表添加了 Python 3 版本。
    • 投票最多的答案仅适用于 ascii 字符串。您的回答明确声称支持 Unicode。
    • @J.F.Sebastian:它适用于 Unicode 字符串。它去除了 ASCII 标点符号。我从未声称它会删除 all 标点符号。 :-) 重点是为 unicode 对象与 Python 2 str 对象提供正确的技术。
    【解决方案10】:

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

    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
    

    【讨论】:

    • 标点符号之间不需要逗号。 punctlist 可以只是一个字符串
    【解决方案11】:

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

    ''.join([c for c in s if c.isalnum() or c.isspace()])
    

    【讨论】:

      【解决方案12】:

      这是 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}))
      

      【讨论】:

        【解决方案13】:

        为了使用方便,我总结了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
        

        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
        

        【讨论】:

        • 有趣的是,这个解决方案(特别是 OR {key: None for...} 选项)允许控制您要插入的内容来代替标点符号,这可能是空格(用于此用途键:“”而不是键:无)。
        【解决方案14】:

        我还没有看到这个答案。只需使用正则表达式;它会删除除单词字符 (\w) 和数字字符 (\d) 之外的所有字符,然后是空格字符 (\s):

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

        【讨论】:

        • \d 是多余的,因为它是 \w 的子集。
        • 数字字符是否被认为是 Word 字符的子集?我认为 Word 字符是可以构造真实单词的任何字符,例如a-zA-Z?
        • 是的,正则表达式中的“单词”包括字母、数字和下划线。请参阅文档中对\w 的描述:docs.python.org/3/library/re.html
        【解决方案15】:
        >>> s = "string. With. Punctuation?"
        >>> s = re.sub(r'[^\w\s]','',s)
        >>> re.split(r'\s*', s)
        
        
        ['string', 'With', 'Punctuation']
        

        【讨论】:

        • 请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
        【解决方案16】:

        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()
        

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

        • 它会删除所有 Unicode 标点符号
        • 它很容易修改,例如如果要删除标点符号,可以删除 \{S},但保留 $ 之类的符号。
        • 您可以非常具体地确定要保留的内容和要删除的内容,例如 \{Pd} 只会删除破折号。
        • 此正则表达式还标准化空白。它将制表符、回车和其他奇怪的东西映射到漂亮的单个空格。

        这使用 Unicode 字符属性,you can read more about on Wikipedia

        【讨论】:

        • 这一行其实不行:remove = regex.compile(ur'[\p{C}|\p{M}|\p{P}|\p{S}|\p{Z}]+', regex.UNICODE)
        • @JohnStud 在 Python 3 的更高版本中中断,因为现在所有字符串都默认支持 unicode。可以从第 2、3 和 4 行中删除“u”并且它可以工作。
        【解决方案17】:

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

        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
        
        • 用空格替换标点符号
        • 用一个空格替换单词之间的多个空格
        • 删除尾随空格,如果有的话 剥离()

        【讨论】:

          【解决方案18】:
          # FIRST METHOD
          # Storing all punctuations in a variable    
          punctuation='!?,.:;"\')(_-'
          newstring ='' # Creating empty string
          word = raw_input("Enter string: ")
          for i in word:
               if(i not in punctuation):
                            newstring += i
          print ("The string without punctuation is", newstring)
          
          # SECOND METHOD
          word = raw_input("Enter string: ")
          punctuation = '!?,.:;"\')(_-'
          newstring = word.translate(None, punctuation)
          print ("The string without punctuation is",newstring)
          
          
          # Output for both methods
          Enter string: hello! welcome -to_python(programming.language)??,
          The string without punctuation is: hello welcome topythonprogramminglanguage
          

          【讨论】:

            【解决方案19】:
            with open('one.txt','r')as myFile:
            
                str1=myFile.read()
            
                print(str1)
            
            
                punctuation = ['(', ')', '?', ':', ';', ',', '.', '!', '/', '"', "'"] 
            
            for i in punctuation:
            
                    str1 = str1.replace(i," ") 
                    myList=[]
                    myList.extend(str1.split(" "))
            print (str1) 
            for i in myList:
            
                print(i,end='\n')
                print ("____________")
            

            【讨论】:

              【解决方案20】:

              使用 Python 从文本文件中删除停用词

              print('====THIS IS HOW TO REMOVE STOP WORS====')
              
              with open('one.txt','r')as myFile:
              
                  str1=myFile.read()
              
                  stop_words ="not", "is", "it", "By","between","This","By","A","when","And","up","Then","was","by","It","If","can","an","he","This","or","And","a","i","it","am","at","on","in","of","to","is","so","too","my","the","and","but","are","very","here","even","from","them","then","than","this","that","though","be","But","these"
              
                  myList=[]
              
                  myList.extend(str1.split(" "))
              
                  for i in myList:
              
                      if i not in stop_words:
              
                          print ("____________")
              
                          print(i,end='\n')
              

              【讨论】:

                【解决方案21】:
                import re
                s = "string. With. Punctuation?" # Sample string 
                out = re.sub(r'[^a-zA-Z0-9\s]', '', s)
                

                【讨论】:

                • 似乎只适用于 ASCII 字符。
                【解决方案22】:

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

                table = str.maketrans({key: None for key in string.punctuation})
                

                对比

                table = str.maketrans('', '', string.punctuation)
                

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

                这是完整的代码:

                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))
                

                这是我的结果:

                sets      : 3.1830138750374317
                sets2      : 2.189873124472797
                regex     : 7.142953420989215
                translate : 4.243278483860195
                translate2 : 2.427158243022859
                replace   : 4.579746678471565
                

                【讨论】:

                  【解决方案23】:

                  为什么你们都不用这个?

                   ''.join(filter(str.isalnum, s)) 
                  

                  太慢了?

                  【讨论】:

                  • 请注意,这也会删除空格。
                  【解决方案24】:

                  考虑 unicode。在 python3 中检查代码。

                  from unicodedata import category
                  text = 'hi, how are you?'
                  text_without_punc = ''.join(ch for ch in text if not category(ch).startswith('P'))
                  

                  【讨论】:

                    【解决方案25】:

                    这是使用 RegEx 的另一种简单方法

                    import re
                    
                    punct = re.compile(r'(\w+)')
                    
                    sentence = 'This ! is : a # sample $ sentence.' # Text with punctuation
                    tokenized = [m.group() for m in punct.finditer(sentence)]
                    sentence = ' '.join(tokenized)
                    print(sentence) 
                    'This is a sample sentence'
                    
                    

                    【讨论】:

                      【解决方案26】:

                      试试那个:)

                      regex.sub(r'\p{P}','', s)
                      

                      【讨论】:

                        【解决方案27】:

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

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

                        【讨论】:

                          【解决方案28】:

                          你也可以这样做:

                          import string
                          ' '.join(word.strip(string.punctuation) for word in 'text'.split())
                          

                          【讨论】:

                            【解决方案29】:

                            这个问题没有太多细节,所以我采取的方法是想出一个对问题最简单解释的解决方案:去掉标点符号。

                            请注意,提出的解决方案不考虑缩略词(例如,you're)或连字符(例如,anal-retentive)...关于它们是否应该被视为标点符号的争论...也不考虑非英语字符集或类似的东西...因为问题中没有提到这些细节。有人认为空格是标点符号,即technically correct...但对我来说,就手头的问题而言,它是零意义的。

                            # using lambda
                            ''.join(filter(lambda c: c not in string.punctuation, s))
                            
                            # using list comprehension
                            ''.join('' if c in string.punctuation else c for c in s)
                            

                            【讨论】:

                              【解决方案30】:

                              显然我无法对所选答案进行编辑,所以这里有一个适用于 Python 3 的更新。translate 方法仍然是进行重要转换时最有效的选择。

                              将最初的繁重工作归功于上面的@Brian。感谢@ddejohn 对改进原始测试的出色建议。

                              #!/usr/bin/env python3
                              
                              """Determination of most efficient way to remove punctuation in Python 3.
                              
                              Results in Python 3.8.10 on my system using the default arguments:
                              
                              set       : 51.897
                              regex     : 17.901
                              translate :  2.059
                              replace   : 13.209
                              """
                              
                              import argparse
                              import re
                              import string
                              import timeit
                              
                              parser = argparse.ArgumentParser()
                              parser.add_argument("--filename", "-f", default=argparse.__file__)
                              parser.add_argument("--iterations", "-i", type=int, default=10000)
                              opts = parser.parse_args()
                              with open(opts.filename) as fp:
                                  s = fp.read()
                              exclude = set(string.punctuation)
                              table = str.maketrans("", "", string.punctuation)
                              regex = re.compile(f"[{re.escape(string.punctuation)}]")
                              
                              def test_set(s):
                                  return "".join(ch for ch in s if ch not in exclude)
                              
                              def test_regex(s):  # From Vinko's solution, with fix.
                                  return regex.sub("", s)
                              
                              def test_translate(s):
                                  return s.translate(table)
                              
                              def test_replace(s):  # From S.Lott's solution
                                  for c in string.punctuation:
                                      s = s.replace(c, "")
                                  return s
                              
                              opts = dict(globals=globals(), number=opts.iterations)
                              solutions = "set", "regex", "translate", "replace"
                              for solution in solutions:
                                  elapsed = timeit.timeit(f"test_{solution}(s)", **opts)
                                  print(f"{solution:<10}: {elapsed:6.3f}")
                              

                              【讨论】:

                              • 这些测试真的没有那么有意义。为了完整起见,为更大的输入字符串添加测试,比如几 KB 的大文本文件。随着s 的增长,str.translate 的发展速度很快。
                              • @ddejohn 好点。我最初的目的是回应那些因无法弄清楚如何让@Brian 的原始脚本在 Python 3.x 下运行而感到沮丧的用户,但我可以看到您的建议显着提高了他的原始测试的价值。事实上,即使是 replace 在以这种方式扩大测试规模时也击败了 regex,而且差距很大。
                              猜你喜欢
                              • 1970-01-01
                              • 2016-02-20
                              • 1970-01-01
                              • 1970-01-01
                              • 2011-06-13
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多