Python Reading and Declaring Variables from a Text File
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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,
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.
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:
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:
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.