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

我无法弄清楚我哪里出错了.因此,我需要随机替换单词并将其重新写入文本文件,直到对其他人不再有意义.我选择了一些单词来测试它,并编写了以下代码,目前无法正常工作:

# 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)

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

现在我们已经完成了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值