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

I am having trouble figuring out where I am going wrong. So I need to accidentally replace words and rewrite them into a text file until it no longer makes sense to anyone else. I picked a few words to test it out and wrote the following code, which does not currently work:

# 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. This design philosophy emphasizes readability of code and its syntax allows programmers to express concepts in fewer lines of code than is possible in languages ​​such as C ++ or Java. The language provides constructs designed to provide clear programs at both small and large scale. Python supports several programming paradigms, including object oriented, imperative and functional programming, or procedural styles. It is equipped with a dynamic type system and automatic memory management and has a large and comprehensive standard library.Python translators are available for installation on many operating systems, allowing you to execute Python code on a wide variety of systems. Using third party tools,such as Py2exe or Pyinstaller, Python code can be packaged as standalone executables for some of the most popular operating systems, distributing Python-based software for use in those environments without the need to install a Python interpreter.

I have tried several methods of this, but as someone new to Python, it is a matter of guessing and doing research on the internet for the last two days, but most of the answers I have found are too hard for me to understand, or are they specific to this human code and don't help me.

+3


source to share


4 answers


OK , take it step by step.

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

      

Better to use it with

here. Also, it r

is the default mode. Thus:

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

      

Using with

will cause it to main.close()

automatically call you when that block ends; you must do the same to write the file at the end.


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:



  • Iterating over a dictionary ( for x in word_replacement

    ) only gives you its keys. Thus, if you want to compare later, you should just check if word_replacement[x] == y

    . Doing it [0]

    on that just gives you the first letter of the replacement.
  • Iterating over a dictionary aims to have the dictionary first. Just loop over the words you want to replace and check if they are in the dictionary using y in word_replacement

    .
  • y == x[1]

    wrong in two . First of all, you would probably like to assign y

    there without comparing (i.e. y = x[1]

    - mark a single sign =

    ). Second, assigning to a loop variable doesn't even do what you want. y

    will just be overwritten with the new value the next time around the loop and the data words

    will NOT be changed at all.

What you want to do is create a new list of replacement words, for example:

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 make some clarification. Dictionaries have a convenience get

method that allows you to get a value if a key is present, or a default if not. If we just use the word itself as the default, we get a great shorthand:

replaced = []
for y in words:
    replacement = word_replacement.get(y, y)
    replaced.append(replacement)
text = ' '.join(replaced)

      

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

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

      

And now we are done.

+7


source


It sounds like you want something like this as an if statement in nested loops:

if x==y:
    y=word_replacement[x]

      

When you iterate over a dictionary, you get its keys, not key-value pairs:

>>> mydict={'Python':'Silly Snake', 'programming':'snake charming', 'system':'table'}
>>> for i in mydict:
...    print i
Python
programming
system

      



Then you can get the value with mydict[i]

.

It doesn't quite work because the assignment y

doesn't change that element words

. You can iterate over your indices instead of elements to assign to the current element:

for x in word_replacement:    
    for y in range(len(words)):
        if x==words[y]:
            words[y]=word_replacement[x]

      

I use range()

and len()

here to get a list of indexes words

( [0, 1, 2, ...]

)

0


source


Your problem is probably here:

if word_replacement[x][0]==y:

      

Here's a small example of what's really going on, which is probably not what you expected:

w = {"Hello": "World", "Python": "Awesome"}
print w["Hello"]
print w["Hello"][0]

      

This should result in:

"World"
"W"

      

You should figure out how to fix the code here.

0


source


You used the wrong path word_replacement

(which is a dictionary). You should change for the loop to something like this:

for y in words:
    if y in word_replacement:
        words[words.index(y)] = word_replacement[y]

      

0


source







All Articles