How to remove punctuation marks from a string in Python 3.x using .translate()?

前端 未结 5 1032
我在风中等你
我在风中等你 2020-12-02 06:16

I want to remove all punctuation marks from a text file using .translate() method. It seems to work well under Python 2.x but under Python 3.4 it doesn\'t seem to do anythin

5条回答
  •  情深已故
    2020-12-02 06:58

    You have to create a translation table using maketrans that you pass to the str.translate method.

    In Python 3.1 and newer, maketrans is now a static-method on the str type, so you can use it to create a translation of each punctuation you want to None.

    import string
    
    # Thanks to Martijn Pieters for this improved version
    
    # This uses the 3-argument version of str.maketrans
    # with arguments (x, y, z) where 'x' and 'y'
    # must be equal-length strings and characters in 'x'
    # are replaced by characters in 'y'. 'z'
    # is a string (string.punctuation here)
    # where each character in the string is mapped
    # to None
    translator = str.maketrans('', '', string.punctuation)
    
    # This is an alternative that creates a dictionary mapping
    # of every character from string.punctuation to None (this will
    # also work)
    #translator = str.maketrans(dict.fromkeys(string.punctuation))
    
    s = 'string with "punctuation" inside of it! Does this work? I hope so.'
    
    # pass the translator to the string's translate method.
    print(s.translate(translator))
    

    This should output:

    string with punctuation inside of it Does this work I hope so
    

提交回复
热议问题