似乎应该有一个比以下更简单的方法:
import string
s = "string. With. Punctuation?" # Sample string
out = s.translate(string.maketrans("",""), string.punctuation)
有吗?
答案 0 :(得分:731)
从效率的角度来看,你不会打败
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)
这比使用每个char的s.replace更快,但是不能像非纯python方法那样运行,例如正则表达式或string.translate,正如您可以从下面的时间中看到的那样。对于这种类型的问题,尽可能低的水平做到这一点是值得的。
时间码:
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
答案 1 :(得分:107)
正则表达式很简单,如果你知道的话。
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
在上面的代码中,我们用空字符串替换(re.sub)所有NON [字母数字字符(\ w)和空格(\ s)]。
因此。和?通过正则表达式运行s变量后,变量's'中不会出现标点符号。
答案 2 :(得分:58)
为了方便使用,我总结了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({key: None for key in string.punctuation})
new_s = s.translate(table) # Output: string without punctuation
答案 3 :(得分:51)
myString.translate(None, string.punctuation)
答案 4 :(得分:25)
我通常使用这样的东西:
>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
... s= s.replace(c,"")
...
>>> s
'string With Punctuation'
答案 5 :(得分:22)
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
答案 6 :(得分:21)
如果你对这个家庭更熟悉的话,不一定更简单,但不一样。
import re, string
s = "string. With. Punctuation?" # Sample string
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
答案 7 :(得分:12)
对于Python 3 str
或Python 2 unicode
值,str.translate()
只接受字典;在该映射中查找代码点(整数),并删除映射到None
的任何内容。
要删除(某些?)标点符号,请使用:
import string
remove_punct_map = dict.fromkeys(map(ord, string.punctuation))
s.translate(remove_punct_map)
dict.fromkeys()
class method使创建映射变得微不足道,根据键序列将所有值设置为None
。
要删除所有标点符号,而不仅仅是ASCII标点符号,您的表格需要更大一些;见J.F. Sebastian's answer(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'))
答案 8 :(得分:12)
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中删除字符串标点符号的最佳方法,因为:
\{S}
,但请保留$
之类的符号。\{Pd}
只会删除破折号。这使用Unicode字符属性,you can read more about on Wikipedia。
答案 9 :(得分:6)
这可能不是最佳解决方案,但这就是我的方法。
import string
f = lambda x: ''.join([i for i in x if i not in string.punctuation])
答案 10 :(得分:6)
这是我写的一个函数。这不是很有效,但它很简单,您可以添加或删除任何您想要的标点符号:
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
答案 11 :(得分:6)
这是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}))
答案 12 :(得分:6)
我还没有看到这个答案。只需使用正则表达式;它会删除除字符(\w
)和数字字符(\d
)之外的所有字符,后跟空格字符(\s
):
import re
s = "string. With. Punctuation?" # Sample string
out = re.sub(ur'[^\w\d\s]+', '', s)
答案 13 :(得分:4)
就像更新一样,我在Python 3中重写了@Brian示例并对其进行了更改以在函数内部移动正则表达式编译步骤。我的想法是为了使功能发挥作用所需的每一步。也许您正在使用分布式计算,并且不能在您的工作人员之间共享正则表达式对象,并且需要在每个工作人员处执行re.compile
步骤。另外,我很想为Python 3的两个不同的maketrans实现计时
table = str.maketrans({key: None for key in string.punctuation})
vs
table = str.maketrans('', '', string.punctuation)
另外我添加了另一种使用set的方法,其中我利用交集函数来减少迭代次数。
这是完整的代码:
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
答案 14 :(得分:4)
这是一个没有正则表达式的解决方案。
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
答案 15 :(得分:3)
import re
s = "string. With. Punctuation?" # Sample string
out = re.sub(r'[^a-zA-Z0-9\s]', '', s)
答案 16 :(得分:3)
>>> s = "string. With. Punctuation?"
>>> s = re.sub(r'[^\w\s]','',s)
>>> re.split(r'\s*', s)
['string', 'With', 'Punctuation']
答案 17 :(得分:2)
在非常严格的情况下,单行可能会有所帮助:
''.join([c for c in s if c.isalnum() or c.isspace()])
答案 18 :(得分:2)
尝试一个:)
regex.sub(r'\p{P}','', s)
答案 19 :(得分:2)
这是使用RegEx进行操作的另一种简便方法
$joined_path2=Join-Path -LiteralPath '\\?\UNC\' -ChildPath $joined_path
答案 20 :(得分:1)
我一直在寻找一个非常简单的解决方案。这是我得到的:
import re
s = "string. With. Punctuation?"
s = re.sub(r'[\W\s]', ' ', s)
print(s)
'string With Punctuation '
答案 21 :(得分:1)
使用正则表达式函数进行搜索和替换,如here.所示。如果你必须重复执行操作,你可以保留正则表达式模式(你的标点符号)的编译副本,这会加快速度。
答案 22 :(得分:1)
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='\n')
print ("____________")
答案 23 :(得分:1)
#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
答案 24 :(得分:0)
为什么你们都不使用这个?
''.join(filter(str.isalnum, s))
太慢了吗?
答案 25 :(得分:0)
考虑unicode。代码已在python3中检查。
df = (df.rename(columns = lambda x: x.replace('_x', ''))
.fillna(df.filter(regex='_y$')
.rename(columns = lambda x: x.replace('_y', '')))
.filter(regex=r'.*(?<!_y)$'))
print (df)
A B C
0 R1 0.0 6.0
1 R2 4.0 8.0
2 R3 2.0 2.0
答案 26 :(得分:0)
您也可以这样做:
import string
' '.join(word.strip(string.punctuation) for word in 'text'.split())
答案 27 :(得分:-1)
这是如何将我们的文档更改为大写 或小写。
print('@@@@This is lower case@@@@')
with open('students.txt','r')as myFile:
str1=myFile.read()
str1.lower()
print(str1.lower())
print('*****This is upper case****')
with open('students.txt','r')as myFile:
str1=myFile.read()
str1.upper()
print(str1.upper())
答案 28 :(得分:-1)
使用Python
从文本文件中删除停用词print('====THIS IS HOW TO REMOVE STOP WORS====')
with open('one.txt','r')as myFile:
str1=myFile.read()
stop_words ="not", "is", "it", "By","between","This","By","A","when","And","up","Then","was","by","It","If","can","an","he","This","or","And","a","i","it","am","at","on","in","of","to","is","so","too","my","the","and","but","are","very","here","even","from","them","then","than","this","that","though","be","But","these"
myList=[]
myList.extend(str1.split(" "))
for i in myList:
if i not in stop_words:
print ("____________")
print(i,end='\n')
答案 29 :(得分:-1)
def punctuationRemover(p):
'''
Input: Takes a string. You may have to use str() to force it.
Removes all punctuation.
Output: Returns a string.
'''
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
no_punctuations = ''
for words in p: # You may not have to loop this high
for char in p:
if char not in punctuations:
no_punctuations = no_punctuations + char
return(no_punctuations)
答案 30 :(得分:-2)
我喜欢使用这样的函数:
def scrub(abc):
while abc[-1] is in list(string.punctuation):
abc=abc[:-1]
while abc[0] is in list(string.punctuation):
abc=abc[1:]
return abc