LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-31-2009, 12:24 AM   #1
dudeman41465
Member
 
Registered: Jun 2005
Location: Kentucky
Distribution: Ubuntu
Posts: 794

Rep: Reputation: 56
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,
 
Old 01-31-2009, 02:43 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
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.
 
Old 01-31-2009, 03:13 AM   #3
dudeman41465
Member
 
Registered: Jun 2005
Location: Kentucky
Distribution: Ubuntu
Posts: 794

Original Poster
Rep: Reputation: 56
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.
 
Old 01-31-2009, 03:17 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by dudeman41465 View Post
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 ?

Last edited by Sergei Steshenko; 01-31-2009 at 03:18 AM.
 
Old 01-31-2009, 11:47 AM   #5
The_Kernel
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
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
 
Old 01-31-2009, 01:43 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by The_Kernel View Post
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 ?
 
Old 01-31-2009, 03:32 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,308

Rep: Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768
Maybe with execfile?
 
Old 01-31-2009, 04:54 PM   #8
The_Kernel
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading variables from other file RudraB Programming 1 03-28-2008 06:06 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
difference between () and = when declaring variables nadroj Programming 1 06-28-2005 01:37 PM


All times are GMT -5. The time now is 05:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration