溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python中去除標(biāo)點(diǎn)符號(hào)的方法

發(fā)布時(shí)間:2020-08-25 10:33:44 來(lái)源:億速云 閱讀:2970 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)python中去除標(biāo)點(diǎn)符號(hào)的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

Python去掉標(biāo)點(diǎn)符號(hào)的方法如下:

方法一:

str.isalnum:

S.isalnum() -> bool

返回值:如果string至少有一個(gè)字符并且所有字符都是字母或數(shù)字則返回True,否則返回False。

實(shí)例:

>>> string = "Special $#! characters   spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'

只能識(shí)別字母和數(shù)字,殺傷力大,會(huì)把中文、空格之類的也干掉

方法二:

string.punctuation

import re, string

s ="string. With. Punctuation?" # Sample string 

# 寫法一:
out = s.translate(string.maketrans("",""), string.punctuation)

# 寫法二:
out = s.translate(None, string.punctuation)

# 寫法三:
exclude = set(string.punctuation)
out = ''.join(ch for ch in s if ch not in exclude)

# 寫法四:
>>> for c in string.punctuation:
			s = s.replace(c,"")
>>> s
'string With Punctuation'

# 寫法五:
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
## re.escape:對(duì)字符串中所有可能被解釋為正則運(yùn)算符的字符進(jìn)行轉(zhuǎn)義

# 寫法六:
# string.punctuation 只包括 ascii 格式; 想要一個(gè)包含更廣(但是更慢)的方法是使用: unicodedata module :
from unicodedata import category
s = u'String — with - ?Punctuation ?...'
out = re.sub('[%s]' % re.escape(string.punctuation), '', s)
print 'Stripped', out
# 輸出:u'Stripped String \u2014 with  \xabPunctuation \xbb'
out = ''.join(ch for ch in s if category(ch)[0] != 'P')
print 'Stripped', out
# 輸出:u'Stripped String  with  Punctuation '


# For Python 3 str or Python 2 unicode values, str.translate() only takes a dictionary; codepoints (integers) are looked up in that mapping and anything mapped to None is removed.
# To remove (some?) punctuation then, use:
import string
remove_punct_map = dict.fromkeys(map(ord, string.punctuation))
s.translate(remove_punct_map)


# Your method doesn't work in Python 3, as the translate method doesn't accept the second argument any more. 
import unicodedata
import sys
tbl = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P'))
def remove_punctuation(text):
	return text.translate(tbl)

方法三:

re

例:

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

測(cè)試:

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): 
	return regex.sub('', s)

def test_trans(s):
	return s.translate(table, string.punctuation)

def test_repl(s):
	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)

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

關(guān)于python中去除標(biāo)點(diǎn)符號(hào)的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI