Python Forum

Full Version: "Statement has no effect"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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?
You need to go back to string formatting; nevermind file read/writing. Don't do this.
Both the unpacking and formatting code are horrendous.
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.
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()
Output:
metulburr@ubuntu:~$ cd save bash: cd: save: No such file or directory
Output:
metulburr@ubuntu:~$ python Python 2.7.12 (default, Nov 19 2016, 06:48:10)  [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from test import DB >>> data = { ...  ...     'entry1':1, ...     'entry2':2 ... } >>> data {'entry2': 2, 'entry1': 1} >>> DB.save(data) >>> exit()
Output:
metulburr@ubuntu:~$ cat save/database2.7.12  {"entry2": 2, "entry1": 1}
Output:
metulburr@ubuntu:~$ python Python 2.7.12 (default, Nov 19 2016, 06:48:10)  [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from test import DB >>> d = DB.load() >>> d {u'entry2': 2, u'entry1': 1}
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.
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.
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, ........}
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.
Do you create this file yourself? Can you change the format?
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?
Pages: 1 2 3