【问题标题】:text.replace(punctuation,'') does not remove all punctuation contained in list(punctuation)?text.replace(punctuation,'') 不删除列表(标点)中包含的所有标点?
【发布时间】:2015-12-14 16:42:18
【问题描述】:
import urllib2,sys
from bs4 import BeautifulSoup,NavigableString
from string import punctuation as p

# URL for Obama's presidential acceptance speech in 2008
obama_4427_url = 'http://www.millercenter.org/president/obama/speeches/speech-4427'

# read in URL
obama_4427_html = urllib2.urlopen(obama_4427_url).read()

# BS magic
obama_4427_soup = BeautifulSoup(obama_4427_html)

# find the speech itself within the HTML
obama_4427_div = obama_4427_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})

# obama_4427_div.text.lower() removes extraneous characters (e.g. '<br/>')
# and places all letters in lowercase
obama_4427_str = obama_4427_div.text.lower()

# for further text analysis, remove punctuation
for punct in list(p):
    obama_4427_str_processed = obama_4427_str.replace(p,'')
obama_4427_str_processed_2 = obama_4427_str_processed.replace(p,'')
print(obama_4427_str_processed_2)

# store individual words
words = obama_4427_str_processed.split(' ')
print(words)

长话短说,我有奥巴马总统的演讲,我希望删除所有标点符号,这样我就只剩下单词了。我已经导入了 punctuation 模块,运行了一个 for 循环,它并没有删除我所有的标点符号。我在这里做错了什么?

【问题讨论】:

  • 否,str.replace() 仅替换 文字
  • for punct in list(p): 你从不使用punct

标签: python string list replace beautifulsoup


【解决方案1】:

如果你想删除标点符号你可以rstrip它关闭:

obama_4427_str = obama_4427_div.text.lower()

# for further text analysis, remove punctuation
from string import punctuation
print([w.rstrip(punctuation) for w in obama_4427_str.split()])

输出:

['transcript', 'to', 'chairman', 'dean', 'and', 'my', 'great', 
'friend', 'dick', 'durbin', 'and', 'to', 'all', 'my', 'fellow', 
'citizens', 'of', 'this', 'great', 'nation', 'with', 'profound', 
'gratitude', 'and', 'great', 'humility', 'i', 'accept', 'your', 
'nomination', 'for', 'the', 'presidency', 'of', 'the', 'united',
................................................................

使用 python3 从任何地方删除使用 str.translate:

from string import punctuation
tbl = str.maketrans({ord(ch):"" for ch in punctuation})
obama_4427_str = obama_4427_div.text.lower().translate(tbl)
print(obama_4427_str.split())

对于python2:

from string import punctuation
obama_4427_str = obama_4427_div.text.lower().encode("utf-8").translate(None,punctuation)
print( obama_4427_str.split())

输出:

['transcript', 'to', 'chairman', 'dean', 'and', 'my', 'great', 
'friend', 'dick', 'durbin', 'and', 'to', 'all', 'my', 'fellow', 
'citizens', 'of', 'this', 'great', 'nation', 'with', 'profound', 
'gratitude', 'and', 'great', 'humility', 'i', 'accept', 'your', 
'nomination', 'for', 'the', 'presidency', 'of', 'the', 'united',
............................................................

另一方面,您可以遍历字符串,因此 list(p) 在您自己的代码中是多余的。

【讨论】:

  • 我正在使用 Python2.7 :(
  • @GBR24,您可以使用python2中的第一个代码,您实际上是在尝试替换任何地方还是只是将其剥离?您还可以使用 encode 在 python2 中使用 translate
【解决方案2】:

str.replace() 搜索第一个参数的整个值。它不是一个模式,所以只有当 whole `string.punctuation* 值存在时,才会用空字符串替换它。

改用正则表达式:

import re
from string import punctuation as p

punctuation = re.compile('[{}]+'.format(re.escape(p)))

obama_4427_str_processed = punctuation.sub('', obama_4427_str)
words = obama_4427_str_processed.split()

请注意,您可以只使用 str.split() 而不使用参数来分割任意宽度的空格,包括换行符。

【讨论】:

  • 当我print(obama_4427_str_processed 时,我没有得到任何读数。我只是把print(obama_4427_str_processed 命令吐给我。如何让它打印整个文本?
  • @GBR24:我很抱歉,我把regexpattern.sub() 的论点弄混了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
相关资源
最近更新 更多