如何使用.translate()从Python 3.x中的字符串中删除标点符号?


77

我想使用.translate()方法从文本文件中删除所有标点符号。它似乎在Python 2.x下工作良好,但在Python 3.4下似乎无能为力。

我的代码如下,输出与输入文本相同。

import string
fhand = open("Hemingway.txt")
for fline in fhand:
    fline = fline.rstrip()
    print(fline.translate(string.punctuation))

Answers:


173

您必须使用maketrans传递给str.translate方法的表来创建翻译表。

在Python 3.1和更高版本中,maketrans现在是str类型静态方法,因此您可以使用它为所需的每个标点创建翻译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))

这应该输出:

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

5
做得很好。不幸的是,该主题的Google最佳搜索结果已被弃用,速度变慢或难以跟踪。
rurp '16

1
似乎string.punctuation不包括引号。我们将如何调整此代码以按键string.punctuation以及用户指定的字符进行裁剪?或语句?
Arash Howaida

1
@ArashHowaidastring.punctuation包括双引号(双引号和单引号)-即使在我的示例中,它也去除了双引号。如果您想自定义哪些内容除了剥离str.punctuation,正好连接string.punctuation你也想去掉,就像一串字符translator = str.maketrans({key: None for key in string.punctuation + 'abc'}),如果你想删除标点和文字的任何事件abc
wkl

我的引号一定有一些编码问题,很高兴知道。谢谢!
Arash Howaida

1
str.maketrans('', '', string.punctuation)也可以。无论如何,都不需要循环,甚至str.maketrans(dict.fromkeys(string.punctuation))在这里会更好。
马丁·彼得斯

25

str.translate的调用签名已更改,并且显然删除了参数deletechars。你可以用

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

而是创建一个表,如其他答案所示。


1
(@birryree示例(stackoverflow.com/a/34294398/1656850)表示不同意您对string.translate的弃权dict令;-)
boardrider

你是对的。在这一点上,我误解了文档。仅呼叫签名已更改:str.translate仅将一个表作为参数,而不再使用deletechars(如birryree的回答所示)。我将相应地编辑答案。
elzell 2015年

这是我发现的唯一兼容Python 2.7 / 3.6的解决方案。我找不到使用对Python 2.7和3.6都适用的translate()的任何解决方案。
临近

23

在python3.x中,可以使用:

import string
#make translator object
translator=str.maketrans('','',string.punctuation)
string_name=string_name.translate(translator)

3

我只是比较了三种方法的速度。translatere.sub预编译慢10倍左右。并且str.replacere.sub大约快3倍。通过str.replace我的意思是:

for ch in string.punctuation:                                                                                                     
    s = s.replace(ch, "'") 

2
我认为您做错了我在Python 3.6.0b4上从stackoverflow.com/a/266162/4249707运行测试(在python3的转换测试部分中采用),并且像许多年前一样,替换了工作。我的结果-集:2.7033574236556888正则表达式:0.9831533581018448翻译:1.837449918501079替换:5.4498765277676284
El Ruso

0

较晚的答案,但是要删除python> = 3.6上的所有标点符号,您还可以使用:

import re, string

clean_string = re.sub(rf"[{string.punctuation}]", "", dirty_string)

演示版

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.