Python Forum
Writing many variables to a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing many variables to a file
#1
I am still a beginner to Python and have the following code to write about 14 variables to a file:
    f.write('{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}\n'.format

            ###################  WRITE THE TAXABLE CATEGORY
            (year, yourage, herage,
             round(begintaxableequity),
             round(endtaxableequity),
             round(begintaxablefixedincome),
             round(fixedincome),
             round(endtaxablefixedincome),
             round(begintaxablecash),
             round(cashincome),
             round(endtaxablecash),
             round(totaltaxableassets),
             round(totaltaxabledivincome),
             taxableequitygwth,
             taxablefixedincomediv,
             taxablecashdiv))
Everything works fine -- no problems.  My questions:

1. If I have say 100 variables, do I follow the same logic/pattern?

2.  Is there a better/faster/simpler way to write many variables?

thanks
Reply
#2
1. You may follow same pattern to complete your task

2. I think a better/faster/simpler way is to have your variables stored in a container (e.g. a list or tuple - which, in a way, you have actually done in "format" already) and loop over that list, each iteration being a write to file

The latter option will also make it a bit easier (and definitely more readable and less error prone) to add/remove variables if needed.
Reply
#3
Hello!
You have many repeating tasks. Make a function pass the original values as parameters or list of parameters and return the rounded values as list. Make a list with all you want and save it as a CSV file. Consider to shorten all the variables names. It's easy to misspell them.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Dec-28-2016, 10:09 PM)birdieman Wrote: Everything works fine -- no problems.  My questions:

1. If I have say 100 variables, do I follow the same logic/pattern?

2.  Is there a better/faster/simpler way to write many variables?

thanks

If you have 100 variables the problem is keeping track of the 100 variables throughout the code, not just at the place where you print them...

What I would so is see if I can find logical groupings of these variables and make object classes from them. Then I can define/code the __str__(self) method in these classes and I just have to give the class instance in a print call and it would output the string with all its components... And these classes will likely have other profitable uses.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File path by adding various variables Mishal0488 2 1,027 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,370 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,002 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,486 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,734 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Writing to file ends incorrectly project_science 4 2,688 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,739 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,413 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 3,784 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,365 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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