Python 2.7 脚本中全局变量的使用
我把“苹果代码”扔进了垃圾桶,请看下面的代码:Python 2.7 脚本中全局变量的使用,python-2.7,variables,dictionary,text-files,Python 2.7,Variables,Dictionary,Text Files,我把“苹果代码”扔进了垃圾桶,请看下面的代码: # Define processing inputted line def inputfile(line): linecontents = { 'item_0110': line[0:8], 'item_0111': line[8:16], 'item_0112': line[16:24] } print 'In the function
# Define processing inputted line
def inputfile(line):
linecontents = { 'item_0110': line[0:8],
'item_0111': line[8:16],
'item_0112': line[16:24] }
print 'In the function : ', linecontents
print 'In the function : ', len(linecontents)
# Set dictionary
linecontents = {}
# Pretend to open a file and read line for line
line = '010012343710203053525150'
# Start processing the read line
inputfile(line)
# Display end resultprint
print '\nOutside the function : ', linecontents
print 'Outside the function : ', len(linecontents)
好吧,首先:我是一个傻瓜,因为我在vars身上试过这个。在最初的帖子中,我已经声明我的文件中有30多个条目(如果你愿意的话,字段)。为了使事情变得更复杂,文件中的一行可以如下所示:
010012343710203053525150
In the function : {'item_0112': '53525150', 'item_0111': '37102030', 'item_0110': '01001234'}
In the function : 3
Outside the function : {}
Outside the function : 0
并不是所有的行都有相同的字段,所以根据字段的类型,我想调用不同的函数
def main():
myfile = 'CustomerFile.txt'
for customer_name, customer_item in read_file(myfile):
if customer_item == 'Apple':
print(customer_name)
else:
print(customer_name + ' is not eating an apple')
现在的问题是:为什么输出是这样的:
010012343710203053525150
In the function : {'item_0112': '53525150', 'item_0111': '37102030', 'item_0110': '01001234'}
In the function : 3
Outside the function : {}
Outside the function : 0
我认为字典独立于函数和/或类?您的代码存在一些问题。我在这里没有看到全局变量的see 我重新格式化并重构了您的代码(除类外,没有大写字母)。你的意图不清楚,所以我尽力了。函数
read_file
实际上逐行读取您的文件,并逐行返回customer_name
和customer_item
def read_file(filepath):
with open(filepath) as customer_file:
for line in customer_file:
customer_name = line[:10]
customer_item = line[10:]
print('Name : ' + customer_name)
print('Item : ' + customer_item)
yield customer_name, customer_item
在main()
或任何函数中,您可以对客户的变量执行您想要的操作
这里重要的是,read\u file
实际读取一个文件,并在将其返回到调用函数之前处理该文件的信息
def main():
myfile = 'CustomerFile.txt'
for customer_name, customer_item in read_file(myfile):
if customer_item == 'Apple':
print(customer_name)
else:
print(customer_name + ' is not eating an apple')
不要使用全局变量,而是让
ReadFile
函数返回值
def ReadFile(line):
...
return CustomerName, CustomerItem
并在调用函数后将其分配给变量:
for line in CustomerFile:
CustomerName, CustomerItem = ReadFile(line)
注意,建议对变量和函数名使用小写字母,并为类保留小写字母。虽然你可以自由地使用自己的风格,但PEP8的使用相当普遍,因此加入PEP8俱乐部后,你的代码将更自然地与其他人的代码“契合”,反之亦然。我找到了(显而易见的)答案。正如我所说,我的Python有点生锈了:
## Define processing inputed line
#
def inputfile(line, linecontents):
linecontents['item_0110'] = line[0:8]
linecontents['item_0111'] = line[8:16]
linecontents['item_0112'] = line[16:24]
print 'In the function : ', linecontents
print 'In the function : ', len(linecontents)
## Define main script
#
def main():
# Set dict
linecontents = {}
# Pretend to open a file and read line for line
line = '010012343710203053525150'
# Start processing the read file
inputfile(line, linecontents)
# Display end resultprint
print '\nOutside the funtion : ', linecontents
print 'Outsude the function : ', len(linecontents)
## Start main script
#
main()
这很好地反映了:
In the function : {'item_0112': '53525150', 'item_0111': '37102030', 'item_0110': '01001234'}
In the function : 3
Outside the funtion : {'item_0112': '53525150', 'item_0111': '37102030', 'item_0110': '01001234'}
Outsude the function : 3
感谢您提供样式指南的链接。至少可以说,我的Python有点生疏了。