【发布时间】:2019-06-14 13:43:42
【问题描述】:
好的,所以这应该不是那么困难,但我正在画一个空白。所以,这个程序的想法是它找到只在文本中出现一次的单词,然后将它们大写。这是完整的代码:
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)
它只打印常规文本,不做任何修改。
事实上,我知道第一部分有效,这只是给我带来麻烦的最后一个 for 循环。
知道我在搞砸什么吗?
【问题讨论】:
标签: python python-3.x string replace