Python: 从字典中替换文本文件中的多个单词

Python: replacing multiple words in a text file from a dictionary

我无法弄清楚哪里出了问题。所以我需要随机替换单词并将它们重新写入文本文件,直到它对其他人不再有意义为止。我选择了一些词来测试它,并编写了以下目前无法运行的代码:

# A program to read a file and replace words until it is no longer understandable

word_replacement = {'Python':'Silly Snake', 'programming':'snake charming', 'system':'table', 'systems':'tables', 'language':'spell', 'languages':'spells', 'code':'snake', 'interpreter':'charmer'}

main = open("INF108.txt", 'r+')

words = main.read().split()

main.close()

for x in word_replacement:    
    for y in words:
        if word_replacement[x][0]==y:
            y==x[1]

text = " ".join(words)

print text

new_main = open("INF108.txt", 'w')
new_main.write(text)
new_main.close()

这是文件中的文本:

Python is a widely used general-purpose, high-level programming language. It's design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.Python interpreters are available for installation on many operating systems, allowing Python code execution on a wide variety of systems. Using third- party tools, such as Py2exe or Pyinstaller, Python code can be packaged into stand-alone executable programs for some of the most popular operating systems, allowing for the distribution of Python-based software for use on those environments without requiring the installation of a Python interpreter.

我已经尝试了几种方法,但作为 Python 的新手,这一直是猜测的问题,过去两天花在网上研究它,但我找到了大部分答案要么太复杂以至于我无法理解,要么特定于那个人的代码并且对我没有帮助。

您似乎想要这样的东西作为嵌套循环中的 if 语句:

if x==y:
    y=word_replacement[x]

当你遍历一个字典时,你得到它的键,而不是键值对:

>>> mydict={'Python':'Silly Snake', 'programming':'snake charming', 'system':'table'}
>>> for i in mydict:
...    print i
Python
programming
system

然后您可以使用 mydict[i] 获取值。

不过这并不完全有效,因为分配给 y 不会更改 words 的那个元素。您可以遍历其索引而不是元素以分配给当前元素:

for x in word_replacement:    
    for y in range(len(words)):
        if x==words[y]:
            words[y]=word_replacement[x]

我在这里使用 range() and len() 来获取 words ([0, 1, 2, ...])

的索引列表

好的,让我们一步步来吧。

main = open("INF108.txt", 'r+')
words = main.read().split()
main.close()

最好在此处使用 with 语句。此外,r 是默认模式。因此:

with open("INF108.txt") as main:
    words = main.read().split()

使用 with 将使 main.close() 在该块结束时自动为您调用;你也应该对最后的文件写入做同样的事情。


现在是主要部分:

for x in word_replacement:    
    for y in words:
        if word_replacement[x][0]==y:
            y==x[1]

这个小部分包含 几个 误解:

  1. 遍历字典 (for x in word_replacement) 只给你它的 keys。因此,当您稍后想要比较时,您应该只检查 if word_replacement[x] == y。在上面做一个 [0] 只会给你替换的第一个 字母
  2. 遍历字典违背了创建字典的初衷。只需遍历要替换的单词,然后使用 y in word_replacement.
  3. 检查 是否在字典中
  4. y == x[1] 两个 方面是错误的。首先,您可能打算在那里分配y,而不是比较(即y = x[1]——注意单个 = 符号)。其次,分配给循环变量甚至不能满足您的要求。 y 将在下次循环时被新值覆盖,而 words 数据根本不会改变。

您要做的是创建一个 可能替换词列表,如下所示:

replaced = []
for y in words:
    if y in word_replacement:
        replaced.append(word_replacement[y])
    else:
        replaced.append(y)
text = ' '.join(replaced)

现在让我们做一些改进。字典有一个方便的 get 方法,可以让您在键存在时获得一个值,或者在键不存在时获得默认值。如果我们只使用 这个词本身 作为默认值,我们会得到一个很好的减少:

replaced = []
for y in words:
    replacement = word_replacement.get(y, y)
    replaced.append(replacement)
text = ' '.join(replaced)

你可以把它变成一行 list-comprehension:

text = ' '.join(word_replacement.get(y, y) for y in words)

现在我们完成了。

您的问题可能出在这里:

if word_replacement[x][0]==y:

这是实际发生的事情的一个小例子,这可能不是您想要的:

w = {"Hello": "World", "Python": "Awesome"}
print w["Hello"]
print w["Hello"][0]

结果应该是:

"World"
"W"

您应该能够从这里弄清楚如何更正代码。

您以错误的方式使用了 word_replacement(这是一本字典)。您应该将 for 循环更改为如下内容:

for y in words:
    if y in word_replacement:
        words[words.index(y)] = word_replacement[y]