从文件导入以使用包含的变量 [英] Importing from a file to use contained variables
问题描述
我正在导入一个包含人名(firstName,middleName等)的文件。如果我定义一个
函数来执行此操作,我如何使用该函数之外的变量?
这是代码:
导入字符串
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])
我需要在另一个函数中使用这些变量(例程)计算相关的数字。
由于这些是函数的本地函数,这是如何完成的?我可以把它们全局化吗?
谢谢,杰夫
" Jeff Wagner" < JW ***** @ hotmail.com>在留言中写道
news:ks ******************************** @ 4ax.com ...
我正在导入一个包含人名(firstName,
middleName等)的文件。如果我定义了一个函数来执行此操作,我该如何使用该函数之外的变量?
您只需从函数返回数据即可。示例:
def returnString():
返回" ABCDE"
def returnInteger():
返回5 + 5
def returnList():
返回[''a'',''B'', 1,3,5]
这是代码:
导入字符串
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])
我需要在另一个函数(例程)中使用这些变量来计算相关的数字。
这个函数是本地的,这是怎么做的?
我可以把它们全局化吗?
全局数据可能是最好的避免[这是一般程序设计
原则,不是特定于Python的东西]。如前面的代码所示,
你可以返回任何数据类型,那么,为什么不以最方便的
形式返回数据,最适合你的目的呢?
您现在有足够的信息来设计解决方案。但是,我可以简单地建议您区分单个实体和实体组b $ b。我的意思是你要编写函数来操作属于单个实体的
数据[一个人的名字]和函数
到操纵人群[你可能希望以后存储
多个人在文件中]。
我希望这有帮助。
Anthony Borla
2003年11月29日星期六05:39:32 GMT,Anthony Borla < AJ ***** @ bigpond.com> wrotf:
Jeff Wagner < JW ***** @ hotmail.com>在消息中写道
新闻:ks ******************************** @ 4ax.com .. < blockquote class =post_quotes>
我正在导入一个包含人名(firstName,
middleName等)的文件。如果我定义了一个函数来执行此操作,我如何才能使用该函数之外的变量?
您只需从函数返回数据即可。示例:
def returnString():
返回" ABCDE"
def returnInteger():
返回5 + 5
def returnList():
返回['''',''B'',1,3,5]
这是代码:
导入字符串
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])
由于这些是本地的函数,这是怎么做的?
可以我把它们变成全球性的东西?
全球数据可能是最好的避免[这是一个更好的程序设计原理,不是Python特有的东西]。如前面的代码所示,
你可以返回任何数据类型,那么,为什么不以最方便的形式返回数据,最适合你的目的呢?
您现在有足够的信息来设计解决方案。但是,我可以简单地建议您区分单个实体和一组实体。通过这个我的意思是你会编写函数来操纵属于单个实体的数据[一个人的名字],以及操作一组人的功能[你可能会我希望稍后将多个人存储在一个文件中。
我希望这会有所帮助。
Anthony Borla
这是我提出的解决方案(也许不是最好的?):
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])
fullName = fName, mName,lName,nName,pName
返回fullName
nameList = getName()
然后当我需要我在另一个模块中使用nameList:
from GetName import nameList
从那里开始。哇,确实有很多值得学习的地方。当我正在阅读一些书籍和教程时,我以为我理解它很好。但是当我开始编写代码时,我似乎已经忘记了所有内容。
现在我正在试图弄清楚如何处理这个列表(我认为它实际上是一个字符串)并获得每个名称的
数值。 />
这是我的字符串:
(''Jeffery'',''Thomas'',''Wagner'',''Jeff' ','''瓦格纳'')
我必须拿出每个名字并加上每个字母代表的内容。
这里''到目前为止我所拥有的:
nameList中每个名字的
:
print eachName #just让我看到每个名字都是正确的..我会删除它后来
for eachName [len(eachName)-1]
调用我的masterNumber例程将它们全部加在一起
和我完全陷入困境。我必须加上J + E + F + F + E + R + Y并将其发送到masterNumber例程以获得
a总计。
For某些原因,当我得到eachName的长度时,它给了我一个值超过正确的
答案,因此,我减去了一个。
这是至少可以说是有挑战性但也很有趣。
杰夫
我想我只是想出了部分问题。
我必须改行:
fullName = fName,mName,lName,nName,pName
to
fullName = fName [: - 1],mName [: - 1],lName [: - 1],nName [: - 1],pName
删除尾随的\ n
这给了我解决方案的一部分;)
杰夫
< blockquote class =post_quotes>这是我提出的解决方案(也许不是最好的?):
def getName():
data = open( enterName.txt)
allNames = data.readlines()
d ata.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])
fullName = fName,mName,lName,nName,pName
返回fullName
nameList = getName()
然后当我在另一个模块中需要nameList时,我使用了:GetName import nameList
从那里开始。哇,确实有很多值得学习的地方。当我读一些书籍和教程时,我以为我理解它很好。但是当我开始编写代码时,我似乎忘了一切。
现在我''我试图弄清楚如何处理这个列表(我认为它实际上是一个字符串)并获得每个名称的
数值。
这是我的字符串:
< (''杰弗瑞'',''托马斯'',''瓦格纳'',''杰夫''''''瓦格纳'')
我必须拿每个名字和加上每个字母代表的内容。
这就是我到目前为止:
nameList中的eachName:
print eachName #just让我看看每个名称都是正确的..我将在以后将其删除
for eachName [len(eachName)-1]
调用我的masterNumber例程将它们全部加在一起
我完全卡住了。我必须加上J + E + F + F + E + R + Y并将其发送到masterNumber例程以获得
由于某种原因,当我得到每个名字的长度,它给了我一个超过正确答案的值,因此,我减去了一个。
这至少可以说是有挑战性的。
<杰夫
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?
Thanks, Jeff
"Jeff Wagner" <JW*****@hotmail.com> wrote in message
news:ks********************************@4ax.com...
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?
You can simply return data from a function. Examples:
def returnString():
return "ABCDE"
def returnInteger():
return 5 + 5
def returnList():
return [''a'', ''B'', 1, 3, 5]
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?
Global data is, probably, best avoided [this is a general program design
principle, not something specific to Python]. As shown in the earlier code,
you can return any data type, so, why not return data in the most convenient
form, the one that best suits your purposes ?
You now have enough enough information to design a solution. May I simply
suggest, however, that you distinguish between a single entity, and a group
of entities. By this I mean you would write function(s) to manipulate the
data belonging to a single entity [the names of a person], and function(s)
to manipulate groups of persons [you will probably want to, later on, store
multiple persons in a file].
I hope this helps.
Anthony Borla
On Sat, 29 Nov 2003 05:39:32 GMT, "Anthony Borla" <aj*****@bigpond.com> wrotf:
"Jeff Wagner" <JW*****@hotmail.com> wrote in message
news:ks********************************@4ax.com.. .
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?
You can simply return data from a function. Examples:
def returnString():
return "ABCDE"
def returnInteger():
return 5 + 5
def returnList():
return [''a'', ''B'', 1, 3, 5]
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?
Global data is, probably, best avoided [this is a general program design
principle, not something specific to Python]. As shown in the earlier code,
you can return any data type, so, why not return data in the most convenient
form, the one that best suits your purposes ?
You now have enough enough information to design a solution. May I simply
suggest, however, that you distinguish between a single entity, and a group
of entities. By this I mean you would write function(s) to manipulate the
data belonging to a single entity [the names of a person], and function(s)
to manipulate groups of persons [you will probably want to, later on, store
multiple persons in a file].
I hope this helps.
Anthony Borla
Here''s the solution I came up with (maybe not the best?):
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])
fullName = fName, mName, lName, nName, pName
return fullName
nameList = getName()
Then when I need nameList in another module, I used:
from GetName import nameList
and went from there. Whew, there sure is a lot to learn. I thought I was understanding it pretty
well when I was reading some books and tutorials but when I started to write code, I seemed to
forget everything.
Now I''m trying to figure out how to process this list (which I think is really a string) and get the
numerical value for each name.
This is my string:
(''Jeffery'', ''Thomas'', ''Wagner'', ''Jeff'', ''Wagner'')
and I have to take each name and add up what each letter represents.
Here''s what I have so far:
for eachName in nameList:
print eachName #just to let me see that each name is correct .. I will remove it later
for eachName[len(eachName)-1]
call my masterNumber routine to add them all together
and I''m totally stuck. I have to add up J+E+F+F+E+R+Y and send it to the masterNumber routine to get
a total.
For some reason, when I got the length of eachName, it was giving me one value over the correct
answer and hence, I subtracted one.
This is challenging to say the least but fun, too.
Jeff
I think I just figured out part of my problem.
I have to change the line:
fullName = fName, mName, lName, nName, pName
to
fullName = fName[:-1], mName[:-1], lName[:-1], nName[:-1], pName
to remove the trailing "\n"
This gives me part of my solution ;)
Jeff
Here''s the solution I came up with (maybe not the best?):
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])
fullName = fName, mName, lName, nName, pName
return fullName
nameList = getName()
Then when I need nameList in another module, I used:
from GetName import nameList
and went from there. Whew, there sure is a lot to learn. I thought I was understanding it pretty
well when I was reading some books and tutorials but when I started to write code, I seemed to
forget everything.
Now I''m trying to figure out how to process this list (which I think is really a string) and get the
numerical value for each name.
This is my string:
(''Jeffery'', ''Thomas'', ''Wagner'', ''Jeff'', ''Wagner'')
and I have to take each name and add up what each letter represents.
Here''s what I have so far:
for eachName in nameList:
print eachName #just to let me see that each name is correct .. I will remove it later
for eachName[len(eachName)-1]
call my masterNumber routine to add them all together
and I''m totally stuck. I have to add up J+E+F+F+E+R+Y and send it to the masterNumber routine to get
a total.
For some reason, when I got the length of eachName, it was giving me one value over the correct
answer and hence, I subtracted one.
This is challenging to say the least but fun, too.
Jeff
这篇关于从文件导入以使用包含的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!