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

前端 未结 5 1030
我在风中等你
我在风中等你 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:54

    The call signature of str.translate has changed and apparently the parameter deletechars has been removed. You could use

    import re
    fline = re.sub('['+string.punctuation+']', '', fline)
    

    instead, or create a table as shown in the other answer.

提交回复
热议问题