"Statement has no effect" - 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: "Statement has no effect" (/thread-1769.html) |
"Statement has no effect" - birdieman - Jan-25-2017 I am still a beginner, but I want to read a file once, do processing, then write to the file. the read code is: f = open("test3.txt", "w") with open("test3.txt", "r") as opened_file: for line in opened_file: MS, StartYear, YourStartAge, HerStartAge, Exempt, BFedItmDed, FedItmDedYrs, State, LivExp, Inflation, Inf1, Inf2, Inf3, Inf4, Inf5, Inf6, Inf7, Inf8, Inf9, Inf10, TESTPcnt, TESTYr, TFISTPcnt, TFISTYr, TFESTPcnt, TFESTYr, TFFISTPcnt, TFFISTYr, TDESTPcnt, TDESTYr, TDFISTPcnt, TDFISTYr, BTE, BTFI, BTC, TERtn, TFIRtn, TCRtn, BTFE, BTFFI, BTFC, TFERtn, TFFIRtn, TFCRtn, BTDE, BTDFI, BTDC, TDERtn, TDFIRtn, TDCRtn, YourJob, SpJob, YourRetInc, SpRetInc, YourSSInc62, YourSSIncFRA, YourSSInc70, SpSSInc62, SpSSIncFRA, SpSSInc70, YourRetAge, SpRetAge, YouBgnRetIncAge, SpBgnRetIncAge, YouSSStartAge, SpSSStartAge, IraStartAge, IraEndAge, IraRequest = line.split()to write the file, I have this code: f.write('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}' '{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}' '{} {} {} {} {} {} {} {} {} {} {} {} {}' .format(MS, StartYear, YourStartAge, HerStartAge, Exempt, BFedItmDed, FedItmDedYrs, State, LivExp, Inflation, Inf1, Inf2, Inf3, Inf4, Inf5, Inf6, Inf7, Inf8, Inf9, Inf10, TESTPcnt, TESTYr, TFISTPcnt, TFISTYr, TFESTPcnt, TFESTYr, TFFISTPcnt, TFFISTYr, TDESTPcnt, TDESTYr, TDFISTPcnt, TDFISTYr, BTE, BTFI, BTC, TERtn, TFIRtn, TCRtn, BTFE, BTFFI, BTFC, TFERtn, TFFIRtn, TFCRtn, BTDE, BTDFI, BTDC, TDERtn, TDFIRtn, TDCRtn, YourJob, SpJob, YourRetInc, SpRetInc, YourSSInc62, YourSSIncFRA, YourSSInc70, SpSSInc62, SpSSIncFRA, SpSSInc70, YourRetAge, SpRetAge, YouBgnRetIncAge, SpBgnRetIncAge, YouSSStartAge, SpSSStartAge, IraStartAge, IraEndAge, IraRequest)) f.close()My PyCharm IDE says the "read" code (highlighting the variables) has no effect. What is wrong with the read code? RE: "Statement has no effect" - Mekire - Jan-25-2017 You need to go back to string formatting; nevermind file read/writing. Don't do this. Both the unpacking and formatting code are horrendous. RE: "Statement has no effect" - birdieman - Jan-25-2017 Thanks for your response, but I am still too new to python to know what your answer means. I had no other idea how to read and write variables to a file. RE: "Statement has no effect" - metulburr - Jan-25-2017 I would suggest to use JSON rather than relying on line position and splitting delimiters. Which in python is basically a dictionary. EDIT: here is an example. Note: this class is using staticmethod to avoid the use of an object, and intends to only handle 1 database due to hardcoding the name in it. test.py import sys import os import json class DB: dirname = 'save' if not os.path.exists(dirname): os.mkdir(dirname) path = os.path.join(dirname, 'database{}'.format(sys.version.split()[0])) @staticmethod def load(): data = open(DB.path) obj = json.load(data) data.close() return obj @staticmethod def save(obj): f = open(DB.path, 'w') f.write(json.dumps(obj)) f.close()
RE: "Statement has no effect" - Mekire - Jan-25-2017 Well, I would rather see something like this (untested): But really as Metul said you should, at bare minimum, probably be using JSON. Honestly it kinda looks like you should be using an actual database. RE: "Statement has no effect" - snippsat - Jan-25-2017 There are also easy solution for fast storage as i show in tutorial with dataset. It's just a couple of lines connet(make database), now trow a Python dictionary into the table and we are finish. Have now a fully working database,that also can be outputted as json. RE: "Statement has no effect" - birdieman - Jan-25-2017 Thanks to all for responding. Metalburr- I still have a lot to learn regarding your example. When it says "entry1:1" I assume the "entry1" (the key) is my variable name, and I assume the "1" is the value of that variable. Is that correct? How/where do I load the database for the first time, like {MS: M, StartYear: 2017, ........} RE: "Statement has no effect" - metulburr - Jan-25-2017 Quote:When it says "entry1:1" I assume the "entry1" (the key) is my variable name, and I assume the "1" is the value of that variable. Is that correct?Yes Quote:How/where do I load the database for the first time, like {MS: M, StartYear: 2017, ........}The easiest method would be to write it in JSON (dictionary) format in the first place. RE: "Statement has no effect" - Kebap - Jan-25-2017 Do you create this file yourself? Can you change the format? RE: "Statement has no effect" - birdieman - Jan-25-2017 yes, i can change the format. After reading Metulburr, and if I understand dictionaries correctly, I would have to refer to my variables like xyz["StartYear"] and I do not want to do that -- too long and cumbersome. I just want to use my variables by their name, like StartYear (if possible). Do you have another suggestion? |