【发布时间】:2021-10-28 03:44:56
【问题描述】:
这是我在这个网站上的第一个问题。请原谅我的任何格式或语言错误。所以这个问题是基于艾伦唐尼的一本名为“think python”的书。活动是编写一个 Python 程序,它以文本格式阅读一本书并删除所有空格,例如空格和制表符以及标点符号和其他符号。我尝试了许多不同的方法来删除标点符号,但它从不删除引号和双引号。他们坚持留下。我将复制粘贴我尝试的最后一个代码。
import string
def del_punctuation(item):
'''
This function deletes punctuation from a word.
'''
punctuation = string.punctuation
for c in item:
if c in punctuation:
item = item.replace(c, '')
return item
def break_into_words(filename):
'''
This function reads file, breaks it into
a list of used words in lower case.
'''
book = open(filename)
words_list = []
for line in book:
for item in line.split():
item = del_punctuation(item)
item=item.lower()
#print(item)
words_list.append(item)
return words_list
print(break_into_words('input.txt'))
我没有包含删除空格的代码,因为它们可以完美运行。我只包含了删除标点符号的代码。除引号和双引号外,所有标点符号都被删除。请通过在代码中找到错误来帮助我,还是与我的 IDE 或编译器有关? 提前致谢
输入.txt:
“Why, my dear, you must know, Mrs. Long says that Netherfield is
taken by a young man of large fortune from the north of England;
that he came down on Monday in a chaise and four to see the
place, and was so much delighted with it that he agreed with Mr.
Morris immediately; that he is to take possession before
Michaelmas, and some of his servants are to be in the house by
the end of next week.”
“What is his name?”
“Bingley.”
“Is he married or single?”
“Oh! single, my dear, to be sure! A single man of large fortune;
four or five thousand a year. What a fine thing for our girls!”
“How so? how can it affect them?”
“My dear Mr. Bennet,” replied his wife, “how can you be so
tiresome! You must know that I am thinking of his marrying one of
them.”
“Is that his design in settling here?”
我得到的输出复制如下:
['“为什么”、“我的”、“亲爱的”、“你”、“必须”、“知道”、“夫人”、“长”、“说”、“那个”、“阴间”、“是','采取','by','a','young','man','of','large','fortune','from','the','north','of' , 'england', 'that', 'he', 'come', 'down', 'on', 'monday', 'in', 'a', 'chaise', 'and', 'four', ' to'、'see'、'the'、'place'、'and'、'was'、'so'、'much'、'delighted'、'with'、'it'、'that'、'he' , '同意', '与', '先生', 'morris', '立即', '那个', '他', '是', 'to', '采取', '占有', '之前', ' michaelmas”、“and”、“some”、“of”、“his”、“servants”、“are”、“to”、“be”、“in”、“the”、“house”、“by” , 'the', 'end', 'of', 'next', 'week'', ''what', 'is', 'his', 'name'', ''bingley'', ''is' , '他', '已婚', '或', '单身', ''哦', '单身', '我的', '亲爱的', 'to', 'be', '确定', 'a' ,'单','人','之','大','财','四','或','五','千','一','年','什么',' a'、'fine'、'thing'、'for'、'our'、'girls''、'how'、'so'、'how'、'can'、'it'、'affect'、'他们”,“我的”,“亲爱的”,“先生”,“班纳特”,“回答','他的','妻子','如何','可以','你','是','所以','累','你','必须','知道','那个','我','我','想','的','他的','结婚','一个','的','他们',''是','那个','他的', '设计', 'in', '定居', '这里'']
它已经删除了除双引号和单引号之外的所有标点符号(我猜输入中有单引号)。 谢谢
【问题讨论】:
-
欢迎来到stackoverflow!虽然您的示例已经非常小,这很好,但它仍然缺少示例输入以及预期和实际输出。否则很难准确地帮助你,因为我们必须猜测到底是什么意思。欲了解更多信息,请阅读有关minimal reproducible examples的页面
-
您使用的输入文本中是否包含“智能引号”?那些有角度的引号不在
string.punctuation中。这些是文字处理器倾向于插入的有角度的引号。 -
如果您“调试”您的代码并“检查”它 - 您的 IDE 将始终显示 " 或 ' 广告开始/结束字符串 - 以明确它是一个字符串。你说的是那些关于?
print()你的项目,看看你是否在你的 cosole 输出中看到它们 -
另外,请使用
with open(...) as而不仅仅是open。 -
您好,非常感谢您的建议和建议。我对我的问题进行了很多改进和更改。请帮帮我。
标签: python string debugging replace quotes