Python:从字典中替换文本文件中的多个单词
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python:从字典中替换文本文件中的多个单词,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3394字,纯文字阅读大概需要5分钟。
内容图文
我无法弄清楚我哪里出错了.因此,我需要随机替换单词并将其重新写入文本文件,直到对其他人不再有意义.我选择了一些单词来测试它,并编写了以下代码,目前无法正常工作:
# 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()在此块结束时自动为您调用;你也应该为最后的文件写做同样的事情.
现在为主要位:
for x in word_replacement:
for y in words:
if word_replacement[x][0]==y:
y==x[1]
这个小部分包含了几个误解:
>迭代字典(对于word_replacement中的x)仅为您提供密钥.因此,当您想稍后进行比较时,您应该检查word_replacement [x] == y.在那上面做[0]只会给你替换的第一个字母.
>迭代字典就是打破了首先使用字典的目的.只需遍历要替换的单词,并使用word_replacement中的y检查它们是否在字典中.
> y == x [1]在两个方面有误.首先,你可能意味着在那里分配y,而不是比较(即y = x [1] – 注意单个=符号).其次,分配给循环变量甚至不能做你想要的. y将在下一次循环中被新值覆盖,并且单词数据将根本不会被更改.
你想要做的是创建一个可能被替换的单词的新列表,如下所示:
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:从字典中替换文本文件中的多个单词全部内容,希望文章能够帮你解决Python:从字典中替换文本文件中的多个单词所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。