如何使用 .translate() 从 Python 3.x 中的字符串中删除标点符号? [英] How to remove punctuation marks from a string in Python 3.x using .translate()?

查看:66
本文介绍了如何使用 .translate() 从 Python 3.x 中的字符串中删除标点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 .translate() 方法从文本文件中删除所有标点符号.它似乎在 Python 2.x 下运行良好,但在 Python 3.4 下似乎没有任何作用.

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 anything.

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

My code is as follows and the output is the same as input text.

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

推荐答案

您必须使用传递给 str.translate 方法的 maketrans 创建一个转换表.

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

在 Python 3.1 和更新版本中,maketrans 现在是一个 静态-str 类型上的方法,因此您可以使用它来创建您想要None的每个标点符号的翻译.

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

这应该输出:

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

这篇关于如何使用 .translate() 从 Python 3.x 中的字符串中删除标点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆