LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python Reading and Declaring Variables from a Text File (http://www.linuxquestions.org/questions/programming-9/python-reading-and-declaring-variables-from-a-text-file-701167/)

dudeman41465 01-31-2009 01:24 AM

Python Reading and Declaring Variables from a Text File
 
Ok, here's basically what I want to do. I want to store variables in a text file, so that I can call them later. Let's say I have the file "variables.txt" and it contains the following lines:
Code:

variable1=value1
variable2=value2

I know how to output data to a text file, however I want to be able to read the variables in my "variables.txt" and declare them so that they stick. Basically I just want to import I guess you could say, the variables in the text file. Can anybody help me out? I'm not an expert with python, so try to break it down for my Barney style, :p

JulianTosh 01-31-2009 03:43 AM

Check out the pickle and shelve modules. They dont store the variables in a text file, but rather a binary representation but at least you're able to store and load variables from a file as you described.

dudeman41465 01-31-2009 04:13 AM

Thanks, I'll have to check it out. I did find out through poking around with it that if you store the variables in a *.py file, and then call it at the beginning you can use the stored variables. Like writing your own mini-module. Example; I store my variables in the file savedata.py which contains:
Code:

strength=5
health=5

Then in the actual program file I could pull those variables with:
Code:

import savedata
print ""
strength=savedata.strength

So you can store the variables in a .py module, import it in your program, and call them with module.variable.

Just figured I'd throw this out there since I kind of found out about it just messing around.

Sergei Steshenko 01-31-2009 04:17 AM

Quote:

Originally Posted by dudeman41465 (Post 3427034)
Thanks, I'll have to check it out. I did find out through poking around with it that if you store the variables in a *.py file, and then call it at the beginning you can use the stored variables. Like writing your own mini-module. Example; I store my variables in the file savedata.py which contains:
Code:

strength=5
health=5

Then in the actual program file I could pull those variables with:
Code:

import savedata
print ""
strength=savedata.strength

So you can store the variables in a .py module, import it in your program, and call them with module.variable.

Just figured I'd throw this out there since I kind of found out about it just messing around.

Now, can you import anonymously ? I.e. without the need to write

Code:

import savedata
?

I mean, without polluting your namespace with "savedata" in this case ?

The_Kernel 01-31-2009 12:47 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3427038)
Now, can you import anonymously ? I.e. without the need to write

Code:

import savedata
?

I mean, without polluting your namespace with "savedata" in this case ?

Yes, I believe
Code:

from savedata import *
will do what your talking about

Sergei Steshenko 01-31-2009 02:43 PM

Quote:

Originally Posted by The_Kernel (Post 3427382)
Yes, I believe
Code:

from savedata import *
will do what your talking about

No, it's not what I'm talking about.

If I take "savedata" from somebody, and that someone wrote

savedata.foo = "bar"

then, I guess, my savedata.foo or foo variable will be clobbered.

It's not what I want.

Does Python have anonymous references ?

ntubski 01-31-2009 04:32 PM

Maybe with execfile?

The_Kernel 01-31-2009 05:54 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3427473)
No, it's not what I'm talking about.

If I take "savedata" from somebody, and that someone wrote

savedata.foo = "bar"

then, I guess, my savedata.foo or foo variable will be clobbered.

It's not what I want.

Does Python have anonymous references ?

Sorry, I'm not familiar with anonymous references. Can you explain? To clarify if you have the variable foo=bar in savedata, running "import savedata" will cause the calling program to have savedata.foo. If you do "from savedata import *" the calling program will have foo. See here for more information: http://docs.python.org/howto/doanddo...-module-import


All times are GMT -5. The time now is 02:26 AM.