我无法弄清楚哪里出错了。所以我需要随机替换单词并将它们重新写入文本文件,直到它对其他人不再有意义为止。我选择了一些词来测试它,并编写了以下当前无法运行的代码:
# 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 的新手,这一直是猜测的问题,过去两天花了在线研究它,但我发现的大多数答案都太复杂了我理解,或者特定于那个人的代码,不要帮助我。
最佳答案
好的,让我们一步一步来。
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()
在此 block 结束时自动为您调用;你也应该对最后的文件写入做同样的事情。
现在是主要部分:
for x in word_replacement:
for y in words:
if word_replacement[x][0]==y:
y==x[1]
这个小部分包含几个错误的概念:
- 遍历字典(
for x in word_replacement
)只为您提供它的键。因此,当您稍后想要比较时,您应该只检查if word_replacement[x] == y
。对其执行[0]
只会为您提供替换的第一个字母。 - 遍历字典违背了创建字典的初衷。只需遍历您要替换的单词,然后使用
y in word_replacement
检查它们是否在字典中。 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)
现在我们完成了。
关于Python:从字典中替换文本文件中的多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008163/