从字符串中去除标点符号的最佳方法

2024-11-22 08:47:00
admin
原创
187
摘要:问题描述:似乎应该有比以下更简单的方法:import string s = "string. With. Punctuation?" # Sample string out = s.translate(string.maketrans("","")...

问题描述:

似乎应该有比以下更简单的方法:

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

有没有?


解决方案 1:

从效率角度来看,你不会打败

s.translate(None, string.punctuation)

对于更高版本的 Python,请使用以下代码:

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

它使用查找表在 C 语言中执行原始字符串操作 - 除了编写自己的 C 代码之外,没有什么能比它更好了。

如果不担心速度的话,另一个选择是:

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

这比对每个字符执行 s.replace 要快,但性能不如正则表达式或 string.translate 等非纯 Python 方法,正如您从下面的时间中看到的那样。对于这种类型的问题,尽可能低地执行是有回报的。

时间代码:

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)

得出以下结果:

sets      : 19.8566138744
regex     : 6.86155414581
translate : 2.12455511093
replace   : 28.4436721802

解决方案 2:

如果您了解正则表达式,那么它就很简单。

import re
s = "string. With. Punctuation?"
s = re.sub(r'[^ws]','',s)

解决方案 3:

为了方便使用,我总结了 Python 2 和 Python 3 中从字符串中删除标点符号的注意事项。详细说明请参考其他答案。


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

解决方案 4:

myString.translate(None, string.punctuation)

解决方案 5:

string.punctuation仅限ASCII !更正确(但也慢得多)的方法是使用 unicodedata 模块:

# -*- coding: utf-8 -*-
from unicodedata import category
s = u'String — with -  «punctation »...'
s = ''.join(ch for ch in s if category(ch)[0] != 'P')
print 'stripped', s

您也可以概括并删除其他类型的字符:

''.join(ch for ch in s if category(ch)[0] not in 'SP')

它还将删除一些字符,~*+§$这些字符可能是也可能不是“标点符号”,这取决于个人的观点。

解决方案 6:

如果您更熟悉 re 家族,则不一定更简单,而是一种不同的方法。

import re, string
s = "string. With. Punctuation?" # Sample string 
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)

解决方案 7:

我通常使用这样的东西:

>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
...     s= s.replace(c,"")
...
>>> s
'string With Punctuation'

解决方案 8:

对于 Python 3str或 Python 2unicode值,str.translate()只需要一个字典;在该映射中查找代码点(整数)并None删除映射到的所有内容。

要删除(一些?)标点符号,请使用:

import string

remove_punct_map = dict.fromkeys(map(ord, string.punctuation))
s.translate(remove_punct_map)

dict.fromkeys()方法使创建映射变得简单,并None根据键的序列设置所有值。

要删除所有标点符号(而不仅仅是 ASCII 标点符号),您的表格需要稍微大一些;请参阅JF Sebastian 的回答(Python 3 版本):

import unicodedata
import sys

remove_punct_map = dict.fromkeys(i for i in range(sys.maxunicode)
                                 if unicodedata.category(chr(i)).startswith('P'))

解决方案 9:

string.punctuation遗漏了大量在现实世界中常用的标点符号。有没有一种适用于非 ASCII 标点符号的解决方案?

import regex
s = u"string. With. Some・Really Weird、Non?ASCII。 「(Punctuation)」?"
remove = regex.compile(ur'[p{C}|p{M}|p{P}|p{S}|p{Z}]+', regex.UNICODE)
remove.sub(u" ", s).strip()

就我个人而言,我认为这是从 Python 中的字符串中删除标点符号的最佳方法,因为:

  • 它删除所有 Unicode 标点符号

  • 它很容易修改,例如{S}如果您想删除标点符号,但保留像这样的符号,您可以删除$

  • 您可以具体说明要保留的内容和要删除的内容,例如{Pd}仅删除破折号。

  • 此正则表达式还会规范化空格。它将制表符、回车符和其他奇怪的字符映射为单个空格。

这使用了 Unicode 字符属性,您可以在 Wikipedia 上阅读更多相关内容。

解决方案 10:

我还没有看到这个答案。只需使用正则表达式;它会删除除单词字符 ( w) 和数字字符 ( d) 之外的所有字符,后跟一个空格字符 ( s):

import re
s = "string. With. Punctuation?" # Sample string 
out = re.sub(ur'[^wds]+', '', s)

解决方案 11:

这可能不是最好的解决方案,但这就是我的做法。

import string
f = lambda x: ''.join([i for i in x if i not in string.punctuation])

解决方案 12:

这是 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}))

解决方案 13:

import re
s = "string. With. Punctuation?" # Sample string 
out = re.sub(r'[^a-zA-Z0-9s]', '', s)

解决方案 14:

这是我写的一个函数。它不是很高效,但很简单,你可以添加或删除任何你想要的标点符号:

def stripPunc(wordList):
    """Strips punctuation from list of words"""
    puncList = [".",";",":","!","?","/","\\\",",","#","@","$","&",")","(","\""]
    for punc in puncList:
        for word in wordList:
            wordList=[word.replace(punc,'') for word in wordList]
    return wordList

解决方案 15:

在不太严格的情况下,一行代码可能会有所帮助:

''.join([c for c in s if c.isalnum() or c.isspace()])

解决方案 16:

>>> s = "string. With. Punctuation?"
>>> s = re.sub(r'[^ws]','',s)
>>> re.split(r's*', s)


['string', 'With', 'Punctuation']

解决方案 17:

作为更新,我用 Python 3 重写了 @Brian 示例,并对其进行了更改,将正则表达式编译步骤移到了函数内部。我的想法是计算使函数工作所需的每个步骤。也许您正在使用分布式计算,无法在工作程序之间共享正则表达式对象,并且需要re.compile在每个工作程序上执行步骤。此外,我很好奇对 Python 3 的 maketrans 的两个不同实现进行计时

table = str.maketrans({key: None for key in string.punctuation})

对比

table = str.maketrans('', '', string.punctuation)

另外,我添加了另一种使用集合的方法,其中我利用交集函数来减少迭代次数。

这是完整的代码:

import re, string, timeit

s = "string. With. Punctuation"


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


def test_set2(s):
    _punctuation = set(string.punctuation)
    for punct in set(s).intersection(_punctuation):
        s = s.replace(punct, ' ')
    return ' '.join(s.split())


def test_re(s):  # From Vinko's solution, with fix.
    regex = re.compile('[%s]' % re.escape(string.punctuation))
    return regex.sub('', s)


def test_trans(s):
    table = str.maketrans({key: None for key in string.punctuation})
    return s.translate(table)


def test_trans2(s):
    table = str.maketrans('', '', string.punctuation)
    return(s.translate(table))


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("sets2      :",timeit.Timer('f(s)', 'from __main__ import s,test_set2 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("translate2 :",timeit.Timer('f(s)', 'from __main__ import s,test_trans2 as f').timeit(1000000))
print("replace   :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000))

这是我的结果:

sets      : 3.1830138750374317
sets2      : 2.189873124472797
regex     : 7.142953420989215
translate : 4.243278483860195
translate2 : 2.427158243022859
replace   : 4.579746678471565

解决方案 18:

我正在寻找一个非常简单的解决方案。以下是我得到的:

import re 

s = "string. With. Punctuation?" 
s = re.sub(r'[Ws]', ' ', s)

print(s)
'string  With  Punctuation '

解决方案 19:

这是一个无需正则表达式的解决方案。

import string

input_text = "!where??and!!or$$then:)"
punctuation_replacer = string.maketrans(string.punctuation, ' '*len(string.punctuation))    
print ' '.join(input_text.translate(punctuation_replacer).split()).strip()

Output>> where and or then
  • 用空格替换标点符号

  • 将单词之间的多个空格替换为单个空格

  • 删除尾随空格(如果有),使用 strip()

解决方案 20:

为什么你们都不用这个?

 ''.join(filter(str.isalnum, s)) 

太慢了?

解决方案 21:

这是使用 RegEx 的另一种简单方法

import re

punct = re.compile(r'(w+)')

sentence = 'This ! is : a # sample $ sentence.' # Text with punctuation
tokenized = [m.group() for m in punct.finditer(sentence)]
sentence = ' '.join(tokenized)
print(sentence) 
'This is a sample sentence'

解决方案 22:

# FIRST METHOD
# Storing all punctuations in a variable    
punctuation='!?,.:;"\')(_-'
newstring ='' # Creating empty string
word = raw_input("Enter string: ")
for i in word:
     if(i not in punctuation):
                  newstring += i
print ("The string without punctuation is", newstring)

# SECOND METHOD
word = raw_input("Enter string: ")
punctuation = '!?,.:;"\')(_-'
newstring = word.translate(None, punctuation)
print ("The string without punctuation is",newstring)


# Output for both methods
Enter string: hello! welcome -to_python(programming.language)??,
The string without punctuation is: hello welcome topythonprogramminglanguage

解决方案 23:

with open('one.txt','r')as myFile:

    str1=myFile.read()

    print(str1)


    punctuation = ['(', ')', '?', ':', ';', ',', '.', '!', '/', '"', "'"] 

for i in punctuation:

        str1 = str1.replace(i," ") 
        myList=[]
        myList.extend(str1.split(" "))
print (str1) 
for i in myList:

    print(i,end='
')
    print ("____________")

解决方案 24:

试试那个:)

regex.sub(r'p{P}','', s)

解决方案 25:

这个问题没有太多细节,所以我采取的方法是对问题进行最简单的解释以提出解决方案:只需删除标点符号。

请注意,所提出的解决方案不考虑缩写词(例如you're)或带连字符的词(例如anal-retentive)……关于它们是否应该被视为标点符号存在争议……也不考虑非英语字符集或类似的东西……因为问题中没有提到这些细节。有人认为空格就是标点符号,这在技术上是正确的……但对我来说,在当前问题的背景下,这毫无意义。

# using lambda
''.join(filter(lambda c: c not in string.punctuation, s))

# using list comprehension
''.join('' if c in string.punctuation else c for c in s)

解决方案 26:

显然我无法对所选答案进行编辑,因此这里有一个适用于 Python 3 的更新。translate在进行非平凡转换时,该方法仍然是最有效的选择。

感谢上述 @Brian 的原创重担。感谢 @ddejohn 对原始测试改进提出的出色建议。

#!/usr/bin/env python3

"""Determination of most efficient way to remove punctuation in Python 3.

Results in Python 3.8.10 on my system using the default arguments:

set       : 51.897
regex     : 17.901
translate :  2.059
replace   : 13.209
"""

import argparse
import re
import string
import timeit

parser = argparse.ArgumentParser()
parser.add_argument("--filename", "-f", default=argparse.__file__)
parser.add_argument("--iterations", "-i", type=int, default=10000)
opts = parser.parse_args()
with open(opts.filename) as fp:
    s = fp.read()
exclude = set(string.punctuation)
table = str.maketrans("", "", string.punctuation)
regex = re.compile(f"[{re.escape(string.punctuation)}]")

def test_set(s):
    return "".join(ch for ch in s if ch not in exclude)

def test_regex(s):  # From Vinko's solution, with fix.
    return regex.sub("", s)

def test_translate(s):
    return s.translate(table)

def test_replace(s):  # From S.Lott's solution
    for c in string.punctuation:
        s = s.replace(c, "")
    return s

opts = dict(globals=globals(), number=opts.iterations)
solutions = "set", "regex", "translate", "replace"
for solution in solutions:
    elapsed = timeit.timeit(f"test_{solution}(s)", **opts)
    print(f"{solution:<10}: {elapsed:6.3f}")

解决方案 27:

处理 Unicode 字符串时,我建议使用PyPiregex模块,因为它同时支持 Unicode 属性类(如p{X}/ P{X})和 POSIX 字符类(如[:name:])。

只需在终端中输入pip install regex(或)并按 ENTER 即可安装该包。pip3 install regex

如果你需要删除任何类型的标点符号和符号(即除字母、数字和空格以外的任何符号),你可以使用

regex.sub(r'[p{P}p{S}]', '', text)  # to remove one by one
regex.sub(r'[p{P}p{S}]+', '', text) # to remove all consecutive punctuation/symbols with one go
regex.sub(r'[[:punct:]]+', '', text)  # Same with a POSIX character class

在线查看Python 演示:

import regex

text = 'भारत India <><>^$.,,! 002'
new_text = regex.sub(r'[p{P}p{S}s]+', ' ', text).lower().strip()
# OR
# new_text = regex.sub(r'[[:punct:]s]+', ' ', text).lower().strip()

print(new_text)
# => भारत india 002

s在这里,我向字符类添加了一个空格模式

解决方案 28:

对于严肃的自然语言处理(NLP),您应该让像SpaCy这样的库通过标记化来处理标点符号,然后您可以根据需要手动进行调整。

例如,您想如何处理单词中的连字符?像缩写这样的特殊情况?开始和结束引号?URL?在 NLP 中,将“let's”这样的缩写分离为“let”和“'s”以便进一步处理通常很有用。

SpaCy 标记化示例

解决方案 29:

考虑unicode。代码在python3中检查。

from unicodedata import category
text = 'hi, how are you?'
text_without_punc = ''.join(ch for ch in text if not category(ch).startswith('P'))

解决方案 30:

您还可以执行以下操作:

import string
' '.join(word.strip(string.punctuation) for word in 'text'.split())
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3878  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2714  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   49  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   49  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   49  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用