Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Statement has no effect"
#1
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?
Reply
#2
You need to go back to string formatting; nevermind file read/writing. Don't do this.
Both the unpacking and formatting code are horrendous.
Reply
#3
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.
Reply
#4
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}
Recommended Tutorials:
Reply
#5
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.
Reply
#6
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.
Reply
#7
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, ........}
Reply
#8
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.
Recommended Tutorials:
Reply
#9
Do you create this file yourself? Can you change the format?
Reply
#10
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing effect sizes for variables in an anova eyavuz21 2 977 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Help with TypeWriter Effect in Python Rich Extra 0 1,169 May-23-2022, 09:44 PM
Last Post: Extra
  Rotation Effect on live Webcam Feed Leziiy 0 1,604 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  How to fix statement seems to have no effect hernancrespo89 0 6,810 Jan-23-2020, 09:23 PM
Last Post: hernancrespo89
  strange effect from duplicating a file descriptor Skaperen 1 2,045 Feb-18-2019, 08:20 PM
Last Post: Skaperen
  Algorithm effect on the CPU Faruk 3 2,610 Dec-19-2018, 08:57 AM
Last Post: Faruk

Forum Jump:

User Panel Messages

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