Verzonden vanaf mijn Samsung Galaxy-smartphone.
-------- Oorspronkelijk bericht --------
Van: boB Stepp <robertvst...@gmail.com>
Datum: 10-05-18 21:53 (GMT+08:00)
Aan: tutor <tutor@python.org>
Onderwerp: Re: [Tutor] Iteration issues
On Wed, May 9, 2018 at 6:27 PM, Roger Lea Scherer <rls...@gmail.com> wrote:
> Hello, again.
>
> I want to count words in a text file. If a word repeats I want to increase
> the count by 1; if the word is new to the dictionary, I want to add the
> word to the dictionary. Everything works like I would like and expect,
> except for it only adds the last word of each line to the dictionary. What
> am I missing?
>
> import string
>
> file_name = 'oxford.txt'
> wordset = {}
> with open(file_name, 'r') as f:
> for line in f:
> sentence = line.strip()
> sentence = sentence.strip(string.punctuation)
> print(sentence)
> sentence = sentence.lower()
> word_list = sentence.strip()
> word_list = word_list.split(' ')
>
> for i in range(len(word_list)):
> word_list[i] = word_list[i].strip(string.punctuation)
I was wondering if you might want to write a small function so that
you can remove all punctuation symbols from each line in one fell
swoop? Something like (in pseudocode):
Iterate over string.punctuation.
If a punctuation symbol is in your string: Replace that symbol with
an empty string.
=>>> maybe something like
import re
no_interpunction = re.sub("[%s]" % re.escape(string.punctuation), '', sentence)
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor