|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to dynamically assign variables
[Python Newbie]
I want to iterate through a file to read lines of pairs of values and dynamically create and set variables. eg line = Variable1 Value1 to create Variable1 with a value of Value1 I don't know what the pairs will contain before reading the file. ConfigParser looks like it will do the job, but I can't figure out how (I don't want the variable to be in a Dict). Using open() and stepping through the lines allows me to grab the 2 items, but I can't work out how to assign a variable based on the 2 items. Any help appreciated |
|
#2
|
||||
|
||||
|
Can you show me an example of what you want, cause i really didnt understand.. sometimes i have this problem understanding users
![]()
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#3
|
|||
|
|||
try this File (a portion of it) Code:
Server_Port 8888 Max_Connect 100 Error_count 20 Code:
def load_from_file():
f = open("c:\\myprog\\progdata.txt","r")
r = f.readlines()
f.close
for line in r:
var, val = line.split()[0], line.split()[1]
so for the first line read I have a variable called var containing Server_port and a variable called val containing 8888. From these I need to create a variable called Server_Port with the value 8888 for Code:
def StartServer():
server = HTTPServer((' ', Server_Port), RequestHandler)
In this case its an INI file, but not always, I don't want to have to list *all* the possible variable names within the prog itself so a dynamic method is preferable. TIA |
|
#4
|
||||
|
||||
|
so you are saing that you are going to have data like so
Code:
Server_Port 8888 Max_Connect 100 Error_count 20 Server_Port 8000 Max_Connect 100 Error_count 20 just want to make sure i fully understand, as im not a python guru yet |
|
#5
|
|||
|
|||
|
What's wrong with having them in a dict? That's the easiest way (and probably the best way) to do it.
|
|
#6
|
|||
|
|||
|
The file will look like that yes (but without duplicates
) |
|
#7
|
||||
|
||||
|
Quote:
Ok give me a few min while i comeup with something for you |
|
#8
|
|||
|
|||
|
Quote:
I'm beginning to think so too Thanks All |
|
#9
|
||||
|
||||
|
ok i got this little sample here.. but cant get rid of them \n
see what you can do.. but it does give you the port numbers ![]() Code:
for x in file('data.db'):
lists = x
Server = lists[lists.find('Server_Port'):20]
print Server[Server.find(' '):]
|
|
#10
|
|||
|
|||
|
This was discussed in a thread here a few weeks ago.
You can assign them to the dictionary returned by the builtin function globals(), and they will become global variables. Code:
def load_from_file():
f = open("c:\\myprog\\progdata.txt","r")
r = f.readlines()
f.close
for line in r:
var, val = line.split()[0], line.split()[1]
globals()[var] = val
However you cannot create local variables this way, since the locals() function creates a copy of the local namespace and any changes to it will have no effect. To create a local variable you have to use Code:
exec "%s = %s" % (var, val) instead. You can also create an attribute on an object with Code:
setattr(obj, var, val) On the other hand, it may be simpler to use a dictionary, as others have said. Dave - The Developers' Coach |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to dynamically assign variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|