Best way to strip punctuation from a string

前端 未结 26 1813
日久生厌
日久生厌 2020-11-21 05:39

It seems like there should be a simpler way than:

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


        
相关标签:
26条回答
  • 2020-11-21 05:55

    From an efficiency perspective, you're not going to beat

    s.translate(None, string.punctuation)
    

    For higher versions of Python use the following code:

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

    It's performing raw string operations in C with a lookup table - there's not much that will beat that but writing your own C code.

    If speed isn't a worry, another option though is:

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

    This is faster than s.replace with each char, but won't perform as well as non-pure python approaches such as regexes or string.translate, as you can see from the below timings. For this type of problem, doing it at as low a level as possible pays off.

    Timing code:

    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)
    

    This gives the following results:

    sets      : 19.8566138744
    regex     : 6.86155414581
    translate : 2.12455511093
    replace   : 28.4436721802
    
    0 讨论(0)
  • 2020-11-21 05:55

    For the convenience of usage, I sum up the note of striping punctuation from a string in both Python 2 and Python 3. Please refer to other answers for the detailed description.


    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
    
    0 讨论(0)
  • 2020-11-21 05:55

    I haven't seen this answer yet. Just use a regex; it removes all characters besides word characters (\w) and number characters (\d), followed by a whitespace character (\s):

    import re
    s = "string. With. Punctuation?" # Sample string 
    out = re.sub(ur'[^\w\d\s]+', '', s)
    
    0 讨论(0)
  • 2020-11-21 05:55

    Try that one :)

    regex.sub(r'\p{P}','', s)
    
    0 讨论(0)
  • 2020-11-21 05:57

    Remove stop words from the text file using 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')
    
    0 讨论(0)
  • 2020-11-21 05:58

    Here's a one-liner for 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}))
    
    0 讨论(0)
提交回复
热议问题