-1

So my python script should get a text file and basically translate it using a dictionary, but I'm stuck and can't get it to work, it runs but doesn't do anything effectively.

1st file (which was given):

# -*- coding: utf-8 -*-

from bead import Ford

szotar = {"The" : "A", "sun": "nap", "shining" : "süt", "wind" : "szél", "not" : "nem", "blowing" : "fúj"}

fd = Ford(szotar)
fd.fordit("teszt.txt", "kimenet.txt")

And my attempt on that Ford class:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Ford:
    def __init__ (self, values = dict(), keys = dict()):
        self.values = values
        self.keys = keys

    def fordit(self, inFile, outFile):
        self.inFile = inFile
        self.outFile = outFile
        try:
           with open("teszt.txt", 'r') as inFile:
               text = inFile.read()
        except:
            print "Nincs input file!"

        for key in dict().iterkeys():
            text.replace(key,dict()[key])

        outFile = open("kimenet.txt", "w")
        outFile.write(text)
        outFile.close()

I am new to python, so every bit of advice and help is greatly appreciated.

M. Adam
  • 53
  • 1
  • 5
  • Are you getting any error message? (Please post it.) Otherwise, explain what "_doesn't do anything effectively_" means. – DYZ May 05 '17 at 03:00
  • 1
    Possible duplicate of [Why doesn't calling a Python string method do anything unless you assign its output?](http://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) – juanpa.arrivillaga May 05 '17 at 03:00
  • Actually, there is more than that wrong with your code. You are iterating over `dict().iterkeys()` but that is an empty dict, so the for-loop never iterates. Also, `dict()[key]` is guaranteed to throw a `KeyError` because once again, you are creating an empty dict on the fly and trying to indext into it – juanpa.arrivillaga May 05 '17 at 03:01

1 Answers1

1

The issue probably lies in the Ford class starting at your __init__ function:

def __init__ (self, values = dict(), keys = dict()):
    self.values = values
    self.keys = keys

What you're doing there is giving Python default values for value and keys, which will both be empty dictionaries if not provided when the function is initialized. Since you're initializing Ford with fd = Ford(szotar), you're basically telling Python that values is the szotar dictionary, but keys is a separate empty dictionary.

Then, in fordit you initialize the function with parameters inFile and outFile, but then read from and write to files without ever using these parameters.

Finally, even if the line text.replace(key,dict()[key]) is getting the right input (which I'm not sure it is), it is not actually editing text — it'd have to look like text = text.replace(key,dict()[key]) instead. This line alone would mean the difference between the output file having the text with the replacements or without them.

I'd rewrite the whole file where you define the Ford class to look like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Ford():
    def __init__ (self, words):
        self.words = words

    def fordit(self, inFile, outFile):
        with open(inFile, 'r') as iF:
            text = iF.read()

        for key in self.words:
            text = text.replace(key, self.words[key]) 

        with open(self.outFile, "w") as oF:
            oF.write(text)

You can also avoid having to call the subfunction fordit manually, by having it look like this instead:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Ford():
    def __init__ (self, words, inFile, outFile):
        self.words = words
        self.inFile = inFile
        self.outFile = outFile
        self.fordit()

    def fordit(self):
        with open(self.inFile, 'r') as iF:
            text = iF.read()

        for key in self.words:
            text = text.replace(key, self.words[key]) 

        with open(self.outFile, "w") as oF:
            oF.write(text)

And then the first file would just need this one line at the bottom, instead of the two you currently have:

Ford(szotar, "teszt.txt", "kimenet.txt")

Note that the string replace method will replace all occurrences of a substring in a string. That means that sun would turn into nap, but sunny would also turn into nappy.

Graham
  • 7,431
  • 18
  • 59
  • 84
Marcy
  • 180
  • 1
  • 12