Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with formatting
#1
Hello, I am having a weird formatting error that is making me not able to run my code. I double checked all the indents to make sure they were 4 spaces. The problem is that it is causing me to get an error saying that file_output = open(output_file, "w+") output_file is not defined, which obviously it is. So this must be some sort of formatting error. It also is saying that that line is in __init__ making me think that for some reason init never actually ends.




class SimpleEncryption:
    file_name = "blank"

    def __init__(self, code_file):
        file_name = code_file
        pass

    def encrypt(self, input_file, output_file):
        file = open(input_file, "r")
        file_output = open(output_file, "w+")
        line = file.readline()
        while line:
            line_words = line.split()
            line_len = len(line_words)
            for x in range(0, line_len):
                file_code = open(file_name, "r")
                line_code = file_code.readline()
                check = 0
                while line_code:
                    line_code_words = line_code.split()
                    if line_code_words[0] == line_words[x]:
                        file_output.write(line_code_words[1])
                        file_output.write(" ")
                        check = 1
                    line_code = file_code.readline()
                if check is 0:
                    file_output.write(line_words[x])
                    file_output.write(" ")
                file_code.close()
                file_code = open(file_name, "r")
            line = file.readline()
            file_output.write("\n")
        file.close()
        file_output.close()


        pass

    def decrypt(self, input_file, output_file) :
        file = open(input_file, "r")
        file_output = open(output_file, "w+")
        line = file.readline()
        while line:
            line_words = line.split()
            line_len = len(line_words)
            for x in range(0, line_len):
                file_code = open(file_name, "r")
                line_code = file_code.readline()
                check = 0
                while line_code:
                    line_code_words = line_code.split()
                    if line_code_words[1] == line_words[x]:
                        file_output.write(line_code_words[0])
                        file_output.write(" ")
                        check = 1
                    line_code = file_code.readline()
                if check is 0:
                    file_output.write(line_words[x])
                    file_output.write(" ")
                file_code.close()
                file_code = open(file_name, "r")
            line = file.readline()
            file_output.write("\n")
        file.close()
        file_output.close()
        pass
Reply
#2
I have the same problem with pycharm
Its always throwing up errors wrt to 4 blank spaces vs tabs.

I can fix it by copying by retyping a substantial part of the code around the error, using the return key, which allows pycharm to decide the indent formatting.
But its a constant pain. If anyone knows the fix for this plz state here!

On return key hit, Pycharm actually often decides to use tabs for indents, but then later complains about tab indents if you alter the code.
Reply
#3
I fixed it, I ended up just retyping the entire thing.
Reply
#4
Hi Hellboy.
I just tried your code and it works for me, except I had to use 'SimpleEncryption.file_name' where you have just used ' file_name ', it is a class variable.

You could try copy pasting this code into a blank python file and see if it works.
Reply
#5
Add the following (replacing file names with your own) at the end of your code
def main():
    code = 'kabelmärking.txt'
    se = SimpleEncryption(code_file=code)
    se.encrypt(input_file='HotelList0.html', output_file='EncryptedHotel.dat')

if __name__ == '__main__':
    main()
Next, file_name must be referenced as self.file_name

next -- you are opening the file on each iteration of your loop
remove to outside of loop so this does not happen
file_code = open(file_name, "r")
put your open's in a with open(...), open(...) as ... loop
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020