Importing from a file to use contained variables
Tim Roberts
timr at probo.com
Sat Nov 29 01:49:07 EST 2003
Jeff Wagner <JWagner at hotmail.com> wrote:
>
>I am importing a file which contains a persons name (firstName, middleName, etc). If I define a
>function to do this, how can I use the variables outside of that function?
>
>Here is the code:
>
>import string
>
>def getName():
>
> data = open("enterName.txt")
> allNames = data.readlines()
> data.close()
>
> fName = string.lower(allNames[0])
> mName = string.lower(allNames[1])
> lName = string.lower(allNames[2])
> nName = string.lower(allNames[3])
> pName = string.lower(allNames[4])
>
>I need to use these variables in another function (routine) to calculate the associated numbers.
>Since these are local to the function, how is this done? Can I make them global or something?
class getName:
def __init__(self, filename):
(
self.fName, self.mName, self.lName, self.nName, self.pName
) = [k.lower() for k in file(filename).readlines()]
names = getName("enterName.txt")
print names.fName
print names.mName
print names.lName
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list