Help with formatting - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with formatting (/thread-1882.html) |
Help with formatting - hellboy632789 - Feb-01-2017 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 RE: Help with formatting - meems - Feb-01-2017 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. RE: Help with formatting - hellboy632789 - Feb-02-2017 I fixed it, I ended up just retyping the entire thing. RE: Help with formatting - meems - Feb-02-2017 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. RE: Help with formatting - Larz60+ - Feb-02-2017 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 |