Python:从字典中替换文本文件中的多个单词 [英] Python: replacing multiple words in a text file from a dictionary

查看:111
本文介绍了Python:从字典中替换文本文件中的多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚哪里出了问题.因此,我需要随机替换单词并将它们重新写入文本文件,直到对其他任何人都不再有意义为止.我选择了一些单词来进行测试,并编写了以下当前不起作用的代码:

I am having trouble figuring out where I'm going wrong. So I need to randomly replace words and re-write them to the text file, until it no longer makes sense to anyone else. I chose some words just to test it, and have written the following code which is not currently working:

# 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是广泛使用的通用高级编程语.它的设计理念强调代码的可读性,语法使程序员可以用更少的代码行来表达概念比诸如C ++或Java这样的语言所能提供的更多.语言提供旨在使小型程序上的程序清晰的构造大规模.Python支持多种编程范例,包括面向对象,命令式和函数式编程,或者程序样式.它具有动态类型系统和自动内存管理,并具有广泛而全面的标准库.Python解释器可用于许多安装操作系统,允许在各种各样的Python代码上执行系统.使用第三方工具,例如Py2exe或Pyinstaller,可以将Python代码打包到独立的可执行程序中,以用于一些最受欢迎的操作系统,允许分发用于这些环境的基于Python的软件无需安装Python解释器.

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的新手,这只是个猜测,而最近两天都在网上进行研究,但是我发现的大多数答案都太复杂了,让我理解或特定于该人员的代码,并且不帮我.

I've tried a few methods of this but as someone new to Python it's been a matter of guessing, and the last two days spent researching it online, but most of the answers I've found are either far too complicated for me to understand, or are specific to that person's code and don't help me.

推荐答案

确定,让我们逐步进行此操作.

OK, let's take this step by step.

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

更好地使用 with 声明在这里.另外, r 是默认模式.因此:

Better to use the with statement here. Also, r is the default mode. Thus:

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

在此块结束时,将一起使用将自动为您调用 main.close();您也应该在文件末尾做同样的事情.

Using with will make main.close() get called automatically for you when this block ends; you should do the same for the file write at the end as well.

现在是主要位:

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

此小节包含几个误解:

  1. 遍历字典(在word_replacement中的x为 )为您提供其 keys .因此,当您以后要进行比较时,应该只检查是否word_replacement [x] == y .对它执行 [0] 只是给您替换的第一个字母.
  2. 遍历字典首先要克服使用字典的目的.只需在要替换的单词上循环,然后使用 y在word_replacement 检查(如果它们在字典中)即可.
  3. y == x [1] 两种方式上是错误的.首先,您可能打算将分配分配给 y ,而不是比较(即 y = x [1] -注意单个 = 符号).其次,分配给循环变量甚至不会做您想要的事情.下次遍历 y 时,它会被新值覆盖,并且 words 数据完全不会更改.
  1. Iterating over a dictionary (for x in word_replacement) gives you its keys only. Thus, when you want to compare later on, you should just be checking if word_replacement[x] == y. Doing a [0] on that just gives you the first letter of the replacement.
  2. Iterating over the dictionary is defeating the purpose of having a dictionary in the first place. Just loop over the words you want to replace, and check if they're in the dictionary using y in word_replacement.
  3. y == x[1] is wrong in two ways. First of all, you probably meant to be assigning to y there, not comparing (i.e. y = x[1] -- note the single = sign). Second, assigning to a loop variable doesn't even do what you want. y will just get overwritten with a new value next time around the loop, and the words data will NOT get changed at all.


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

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

现在让我们进行一些改进.字典有一个方便的 get 方法如果存在键,则可以获取一个值;如果不存在,则可以获取一个默认值.如果我们只使用单词本身作为默认值,则会得到一个很好的简化:

Now let's do some refinement. Dictionaries have a handy get method that lets you get a value if the key is present, or a default if it's not. If we just use the word itself as a default, we get a nifty reduction:

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)

现在我们完成了.

这篇关于Python:从字典中替换文本文件中的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆