Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

This is the text in the file:

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.

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.

like image 512
Hatricia Avatar asked Oct 23 '25 06:10

Hatricia


1 Answers

OK, let's take this step by step.

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

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

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

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.


Now for the main bit:

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

This little section has several misconceptions packed into it:

  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.

What you want to do is create a new list of possibly-replaced words, like so:

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

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)

Which you can just turn into a one-line list-comprehension:

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

And now we're done.

like image 179
tzaman Avatar answered Oct 25 '25 20:10

tzaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!