0

Ok, so this shouldn't be nearly this difficult, but I'm drawing a blank. So, the idea of this program is that it finds words that only occur in a text once, then capitalizes them. Here's the complete code:

from collections import Counter
from string import punctuation

path = input("Path to file: ")
with open(path) as f:
    word_counts = Counter(word.strip(punctuation) for line in f for word in line.replace(")", " ").replace("(", " ")
                      .replace(":", " ").replace("", " ").split())

wordlist = open(path).read().replace("\n", " ").replace(")", " ").replace("(", " ").replace("", " ")

unique = [word for word, count in word_counts.items() if count == 1]

for word in unique:
    text = wordlist
    text.replace(word, str(word.upper()))

print(text)

It just prints the regular text, with no modifications made.

I know for a fact the first part works, It's just the final for loop thats giving me trouble.

Any idea what I'm screwing up?

Julia Fasick
  • 121
  • 7

2 Answers2

4

Replace this line

text.replace(word, str(word.upper()))

with

text = text.replace(word, str(word.upper()))

string.replace() does not modify the original string instance.

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 1
    AHHH. thank you so much I'll accept it as answer as soon as I'm allowed to! – Julia Fasick Jun 14 '19 at 13:52
  • ok, so one more thing. when the expected output should be `Genesis 37:1 Jacob lived In the land of his father's SOJOURNINGS, in the land of Canaan.`, it prints `GenesIs 37:1 Jacob lIved In the land of hIs FATher's SOJOURNINGS, In the land of Canaan. ` I believe this is because of the fact that the capitalized letters appear in other keywords. Is there anyway to get around this? – Julia Fasick Jun 14 '19 at 14:31
  • Please open a new question for this with a minimal working example. – buhtz Jun 14 '19 at 14:34
1

You should assign it back to text.

text = text.replace(word, str(word.upper()))
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38