• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Dynamically assigning variable names in Python

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

jhanby

Member
Joined
Aug 7, 2007
Location
Peterborough, UK
I've started writing small Python programs and run into a problem.

I'm reading from a file which contains all of the user details for the program.

The file is made up of one line per user, where the first field in each line is the username.

I am using writer.readlines() to read the file, then assign each entry in the list it creates to a variable.

However, I would like the variables to be called '1', '2', '3', etc etc. I cannot find a way to tell Python to increment the name of the variable every time it reads a line.

I'm at work right now so can't post the exact code, but will do when I get home tonight.

John
 

Cowboy Shane

Member
Joined
Jan 9, 2002
Location
Augusta, GA
Any reason you don't want to use a list to store the usernames? Then, you have things like username[0], username[1],..., etc. Plus, you don't have to keep your own count.
 

SeanBest

Member
Joined
Aug 14, 2006
Location
Harrisonburg, VA
Yeah storing anything that's changing like that you should use and Array, only if you know the exact amount of entries, or ArrayList (coming from Java here so not sure what Python calls it).
 
OP
jhanby

jhanby

Member
Joined
Aug 7, 2007
Location
Peterborough, UK
Thanks for both of yours help.

I've thought about it again and have decided to do the following.

First, Jazz on ...

1. Write the config file like this :
user|pass|dir|dbdir|user|pass|dir|dbdir| etc...
2. Read this using
Code:
writer.read()
3. Split it using
Code:
string.split('|')
, which will create a long list of everything
5. Count how many items in list, divide by 4,
Code:
len(list)\4
6. use a while loop to append to a new list, where the position increases evey time
7. Repeat for every option.

That way, I can know that the first in each list corresponds, etc ...

How does that sound ?

Yeah storing anything that's changing like that you should use and Array, only if you know the exact amount of entries, or ArrayList (coming from Java here so not sure what Python calls it).

From that I think an array is a Tuple (apart from a tuple is immutable) and a ArrayList is a list.
 

Omsion

Member
Joined
Mar 6, 2006
Why not nested list?

Code:
userinfo = []
lines = file.readlines()	##lines is list of lines
for each line in lines:
    line = line.strip()		##gets rid of EOL spacing, newlines, etc
    data = line.split()		##by default, splits by whitespace - need an argument; returns list
    userinfo.append(data)	##append to list of all data

From that I think an array is a Tuple (apart from a tuple is immutable) and a ArrayList is a list.
Tuples are immutable, correct. There are no fixed length arrays in python, and they're called lists.
 
OP
jhanby

jhanby

Member
Joined
Aug 7, 2007
Location
Peterborough, UK
Why not nested list?

Code:
userinfo = []
lines = file.readlines()	##lines is list of lines
for each line in lines:
    line = line.strip()		##gets rid of EOL spacing, newlines, etc
    data = line.split()		##by default, splits by whitespace - need an argument; returns list
    userinfo.append(data)	##append to list of all data

Surely this is like doing steps one to three in my post, as won't this give me a long list of every 'field' of my config file ?.

I would still then need to pick out the data (every 4th field for username, so on ...). This is what I've come up with, Let me know if its long winded :
Code:
configfileread= open('/home/john/Desktop/test/files/configfile')
cfcontent = configfileread.read()
cfcontentlist = cfcontent.split('|')
cflen = len(cfcontentlist)
amountusers = cflen / 4
## Username writing
tmp = 0
usernamelist = []
usersofar = 0
while tmp < amountusers:
        usernamelist.append(cfcontentlist[usersofar])
        usersofar = usersofar + 4
        tmp = tmp + 1

I know it is quite cumbersome, but I like my while loops :D

EDIT : oops, fixed a silly mistake :p (caused my comp to crash by going into an infinite while loop :D)

EDIT 2 : I'm guessing that if I had used some sort of for, I wouldn't have ever got into an infinite loop. I'm not great with For's though..

EDIT 3 : This code works, but does create 51 extra lines of code. Now to change my login bit to check in the lists....
 
Last edited:

Omsion

Member
Joined
Mar 6, 2006
So you just want the user names? And they're the first thing?
Code:
usernames = []
lines = file.readlines()	##lines is list of lines (strings)
for each line in lines:
    line = line.strip()		##gets rid of EOL spacing, newlines, etc
    data = line.split('|")	##returns list
    userinfo.append(data[0])	##append user name
KISS is what python is really good at - but your code will work, although you'll have quite the messy config file
 
OP
jhanby

jhanby

Member
Joined
Aug 7, 2007
Location
Peterborough, UK
So you just want the user names? And they're the first thing?
Code:
usernames = []
lines = file.readlines()	##lines is list of lines (strings)
for each line in lines:
    line = line.strip()		##gets rid of EOL spacing, newlines, etc
    data = line.split('|")	##returns list
    userinfo.append(data[0])	##append user name
KISS is what python is really good at - but your code will work, although you'll have quite the messy config file

Genius. This is going to mean less variables and no infinite loops.

Thank you

I'll share my program when its done :)