Melhor maneira de faixa de pontuação de uma string
-
06-07-2019 - |
Pergunta
Parece que deve haver uma maneira mais simples do que:
import string
s = "string. With. Punctuation?" # Sample string
out = s.translate(string.maketrans("",""), string.punctuation)
Existe?
Solução
Do ponto de vista da eficiência, você não está indo para vencer
s.translate(None, string.punctuation)
Para versões mais elevados de Python usar o seguinte código:
s.translate(str.maketrans('', '', string.punctuation))
Ele está realizando operações de cadeia matérias em C com uma tabela de pesquisa - não há muito que vai bater isso, mas escrever seu próprio código C
.Se a velocidade não é uma preocupação, uma outra opção, porém, é:
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)
Esta é mais rápido do que s.replace com cada char, mas não irá executar, bem como python não-puro abordagens como expressões regulares ou string.translate, como você pode ver a partir do abaixo horários. Para este tipo de problema, fazê-lo em um nível tão baixo quanto possível compensa.
código Timing:
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)
Isto dá os seguintes resultados:
sets : 19.8566138744
regex : 6.86155414581
translate : 2.12455511093
replace : 28.4436721802
Outras dicas
As expressões regulares são bastante simples, se você conhecê-los.
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
No código acima, estamos substituindo (re.sub) todos os não [caracteres alfanuméricos (\ w) e espaços (\ s)] com string vazia.
Conseqüentemente . e? pontuação não vai estar presente na variável 's' depois de executar s variável através de regex.
Para a conveniência de uso, eu resumir a nota de striping pontuação de uma corda em ambos Python 2 e Python 3. Por favor, consulte outras respostas para a descrição detalhada.
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
myString.translate(None, string.punctuation)
Eu costumo usar algo como isto:
>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
... s= s.replace(c,"")
...
>>> s
'string With Punctuation'
string.punctuation
é ASCII única ! A mais correta (mas também muito mais lento) maneira é usar o módulo 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
Não necessariamente mais simples, mas de forma diferente, se você está mais familiarizado com a família re.
import re, string
s = "string. With. Punctuation?" # Sample string
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
Para Python 3 str
ou Python 2 valores unicode
, str.translate()
leva apenas um dicionário; codepoints (inteiros) são procurados em que o mapeamento e tudo mapeado para None
é removida.
Para remover (alguns?) Pontuação, em seguida, use:
import string
remove_punct_map = dict.fromkeys(map(ord, string.punctuation))
s.translate(remove_punct_map)
O dict.fromkeys()
método de classe torna trivial para criar o mapeamento, definindo todos os valores para None
com base na sequência de teclas.
Para remover todas pontuação, não apenas a pontuação ASCII, suas necessidades de tabela para ser um pouco maior; consulte J.F. de Sebastian resposta (Python 3 versão):
import unicodedata
import sys
remove_punct_map = dict.fromkeys(i for i in range(sys.maxunicode)
if unicodedata.category(chr(i)).startswith('P'))
string.punctuation
perde um monte de marcas de pontuação que são comumente usados ??no mundo real. Como sobre uma solução que funciona para a pontuação não-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()
Pessoalmente, eu acredito que esta é a melhor maneira para remover a pontuação de uma string em Python porque:
- Ele remove todos os Unicode pontuação
- É facilmente modificáveis, por exemplo, você pode remover o
\{S}
se você deseja remover a pontuação, mas manter símbolos como$
. - Você pode ficar muito específico sobre o que você deseja manter eo que você deseja remover, por exemplo
\{Pd}
só irá remover traços. - Este regex também normaliza espaço em branco. Ele mapeia guias, retornos de carro, e outras esquisitices para Nice, espaços simples.
Isto usa as propriedades de caracteres Unicode, que você pode ler mais sobre a Wikipedia .
Isto pode não ser a melhor solução no entanto, isso é como eu fiz isso.
import string
f = lambda x: ''.join([i for i in x if i not in string.punctuation])
Aqui está uma função que eu escrevi. Não é muito eficiente, mas é simples e você pode adicionar ou remover qualquer pontuação que você deseja:
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
Aqui está um one-liner para 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}))
Eu não vi essa resposta ainda. Basta usar um regex; ele remove todos os caracteres, além de caracteres de palavras (\w
) e caracteres numéricos (\d
), seguido por um espaço em branco (\s
):
import re
s = "string. With. Punctuation?" # Sample string
out = re.sub(ur'[^\w\d\s]+', '', s)
Aqui está uma solução sem regex.
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
- Substitui as pontuações com espaços
- Substitua múltiplos espaços entre as palavras com um espaço único
- Remova os espaços à direita, se houver, com tira ()
Assim como uma atualização, eu reescrevi o exemplo @ Brian em Python 3 e fez alterações para que ele se mova regex dentro etapa de compilação da função. Meu pensamento aqui era tempo de cada passo necessário para fazer o trabalho função. Talvez você estiver usando computação distribuída e não pode ter objeto regex compartilhada entre os seus trabalhadores e necessidade de ter etapa re.compile
a cada trabalhador. Além disso, eu estava curioso para tempo de duas implementações diferentes de maketrans para Python 3
table = str.maketrans({key: None for key in string.punctuation})
vs
table = str.maketrans('', '', string.punctuation)
Além disso, eu adicionei um outro método para definir o uso, onde eu tirar proveito da função cruzamento para reduzir o número de iterações.
Este é o código completo:
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))
Este é meus resultados:
sets : 3.1830138750374317
sets2 : 2.189873124472797
regex : 7.142953420989215
translate : 4.243278483860195
translate2 : 2.427158243022859
replace : 4.579746678471565
>>> s = "string. With. Punctuation?"
>>> s = re.sub(r'[^\w\s]','',s)
>>> re.split(r'\s*', s)
['string', 'With', 'Punctuation']
import re
s = "string. With. Punctuation?" # Sample string
out = re.sub(r'[^a-zA-Z0-9\s]', '', s)
Um one-liner pode ser útil em casos não muito estritas:
''.join([c for c in s if c.isalnum() or c.isspace()])
#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
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 ("____________")
palavras Remove parada do arquivo de texto usando 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')
Este é como mudar seus documentos para maiúsculas ou minúsculas.
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())
Eu gosto de usar uma função como esta:
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