Najlepszy sposób na usunięcie interpunkcji ze Sznurka

Wydaje się, że powinien być prostszy sposób niż:

import string
s = "string. With. Punctuation?" # Sample string 
out = s.translate(string.maketrans("",""), string.punctuation)
Jest?
Author: Georgy, 2008-11-05

26 answers

Z punktu widzenia efektywności, nie pokonasz

s.translate(None, string.punctuation)

Dla wyższych wersji Pythona użyj następującego kodu:

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

Wykonuje nieprzetworzone operacje łańcuchowe w C z tabelą wyszukiwania - nie ma nic, co by to przebiło, ale pisanie własnego kodu w C.

Jeśli prędkość nie jest zmartwieniem, inną opcją jest jednak:

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

Jest to szybsze niż s.replace z każdym znakiem, ale nie będzie działać tak dobrze, jak nie-czyste podejścia Pythona, takie jak wyrażenia regularne lub sznurek.Przetłumacz, jak widać z poniższych terminów. W przypadku tego typu problemów opłaca się robić to na jak najniższym poziomie.

Kod czasu:

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)

Daje to następujące wyniki:

sets      : 19.8566138744
regex     : 6.86155414581
translate : 2.12455511093
replace   : 28.4436721802
 1043
Author: Brian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-05 21:07:37

Wyrażenia regularne są dość proste, jeśli je znasz.

import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
 167
Author: Eratosthenes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-16 17:36:50

Dla wygody użytkowania, podsumuję notatkę o wycinaniu interpunkcji z ciągu znaków zarówno w Pythonie 2, jak i Pythonie 3. Proszę zapoznać się z innymi odpowiedziami do szczegółowego opisu.


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
 75
Author: SparkAndShine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-10-09 00:54:11
myString.translate(None, string.punctuation)
 52
Author: pyrou,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-08 15:19:09

Zwykle używam czegoś takiego:

>>> s = "string. With. Punctuation?" # Sample string
>>> import string
>>> for c in string.punctuation:
...     s= s.replace(c,"")
...
>>> s
'string With Punctuation'
 32
Author: S.Lott,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-11-05 17:41:27

string.punctuation is ASCII only! Poprawniejszym (ale i znacznie wolniejszym) sposobem jest użycie modułu 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

Można uogólniać i usuwać również inne typy znaków:

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

Usunie również znaki takie jak ~*+§$, które mogą lub nie mogą być "interpunkcją" w zależności od punktu widzenia.

 29
Author: Björn Lindqvist,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-02 03:54:35

Niekoniecznie prostsze, ale w inny sposób, jeśli jesteś bardziej zaznajomiony z rodziną re.

import re, string
s = "string. With. Punctuation?" # Sample string 
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
 26
Author: Vinko Vrsalovic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-11-05 23:20:04

Dla wartości Python 3 str lub Python 2 unicode, str.translate() zajmuje tylko słownik; Punkty kodowe (liczby całkowite) są sprawdzane w tym mapowaniu i wszystko, co zmapowane do None jest usuwane.

Aby usunąć (niektóre?) interpunkcja następnie, użyj:

import string

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

The dict.fromkeys() metoda class sprawia, że tworzenie mapowania jest trywialne, ustawiając wszystkie wartości na None na podstawie sekwencji kluczy.

Aby usunąć wszystkie interpunkcje, a nie tylko interpunkcję ASCII, twoja tabela musi być trochę większy; zobacz odpowiedź J. F. Sebastiana (wersja Pythona 3):

import unicodedata
import sys

remove_punct_map = dict.fromkeys(i for i in range(sys.maxunicode)
                                 if unicodedata.category(chr(i)).startswith('P'))
 13
Author: Martijn Pieters,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:47

string.punctuation pomija mnóstwo znaków interpunkcyjnych, które są powszechnie używane w świecie rzeczywistym. Co powiesz na rozwiązanie, które działa dla interpunkcji innych niż 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()

Osobiście uważam, że jest to najlepszy sposób na usunięcie interpunkcji z ciągu znaków w Pythonie, ponieważ:

  • usuwa wszystkie znaki interpunkcyjne Unicode
  • można łatwo modyfikować, np. można usunąć \{S}, jeśli chcesz usunąć znaki interpunkcyjne, ale zachować symbole jak $.
  • można uzyskać naprawdę konkretne o tym, co chcesz zachować i to, co chcesz usunąć, na przykład \{Pd} usunie tylko myślniki.
  • Ten regex również normalizuje białe znaki. Mapuje karty, powroty karetki i inne dziwactwa do ładnych, pojedynczych przestrzeni.

Używa właściwości znaków Unicode, o których możesz przeczytać więcej na Wikipedii .

 12
Author: Zach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-15 08:17:10

Oto jednolinijkowy dla Pythona 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}))
 9
Author: Tim P,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-15 08:13:41

Nie widziałem jeszcze tej odpowiedzi. Po prostu użyj wyrażenia regularnego; usuwa wszystkie znaki oprócz znaków słownych (\w) i znaków liczbowych (\d), po których następuje znak spacji (\s):

import re
s = "string. With. Punctuation?" # Sample string 
out = re.sub(ur'[^\w\d\s]+', '', s)
 9
Author: Blairg23,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-15 08:15:19

To może nie być najlepsze rozwiązanie, jednak tak to zrobiłem.

import string
f = lambda x: ''.join([i for i in x if i not in string.punctuation])
 8
Author: David Vuong,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-02 10:28:30

Oto funkcja, którą napisałem. Nie jest zbyt wydajny, ale jest prosty i możesz dodać lub usunąć dowolną interpunkcję, której pragniesz:

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
 6
Author: Dr.Tautology,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-15 08:12:48
import re
s = "string. With. Punctuation?" # Sample string 
out = re.sub(r'[^a-zA-Z0-9\s]', '', s)
 5
Author: Haythem HADHAB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-02 14:58:39

Jako aktualizacja, przepisałem przykład @ Brian w Pythonie 3 i wprowadziłem w nim zmiany, aby przenieść krok kompilacji regex wewnątrz funkcji. Myślałem, że czas na każdy krok potrzebny, aby funkcja działała. Być może używasz komputerów rozproszonych i nie możesz mieć obiektu regex współdzielonego między pracownikami i musisz mieć re.compile krok przy każdym pracowniku. Ponadto, byłem ciekawy czasu dwie różne implementacje maketrans dla Pythona 3

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

Vs

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

Plus I dodano inną metodę do użycia set, gdzie korzystam z funkcji przecięcia, aby zmniejszyć liczbę iteracji.

Oto kompletny kod:

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

Oto moje wyniki:

sets      : 3.1830138750374317
sets2      : 2.189873124472797
regex     : 7.142953420989215
translate : 4.243278483860195
translate2 : 2.427158243022859
replace   : 4.579746678471565
 5
Author: krinker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-07 14:25:26
>>> s = "string. With. Punctuation?"
>>> s = re.sub(r'[^\w\s]','',s)
>>> re.split(r'\s*', s)


['string', 'With', 'Punctuation']
 4
Author: Pablo Rodriguez Bertorello,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-24 05:43:58

Oto rozwiązanie bez 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
  • Zastępuje znaki interpunkcyjne spacjami
  • Zastąp wiele spacji pomiędzy wyrazami pojedynczą spacją
  • Usuń spacje końcowe, jeśli występują z strip ()
 4
Author: ngub05,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-02 20:03:45

Jednowierszowy może być pomocny w niezbyt surowych przypadkach:

''.join([c for c in s if c.isalnum() or c.isspace()])
 4
Author: Dom Grey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-15 08:13:25
#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
 2
Author: Animeartist,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-02 08:56:57
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 ("____________")
 2
Author: Isayas Wakgari Kelbessa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-02 20:10:23

Oto jeszcze jeden prosty sposób, aby to zrobić za pomocą 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'

 2
Author: Zain Sarwar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-08-20 15:07:01

Spróbuj tego:)

regex.sub(r'\p{P}','', s)
 2
Author: Vivian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-09-02 07:51:45

Dlaczego nikt z Was tego nie używa?

 ''.join(filter(str.isalnum, s)) 
Za wolno?
 1
Author: Dehua Li,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-29 08:18:31

Biorąc pod uwagę unicode. Kod sprawdzony w 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'))
 0
Author: Rajan saha Raju,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-04 05:14:11

Usuń słowa stop z pliku tekstowego za pomocą Pythona

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')
 -1
Author: Isayas Wakgari Kelbessa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-05 08:47:40

Lubię używać takiej funkcji:

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
 -2
Author: Disk Giant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-06 17:28:57