我把'Apple-code'扔进垃圾桶,请看下面的代码:
# 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)
好的,首先关闭:我是个笨蛋试试这个。在我已经说过的原始帖子中,我在文件中有超过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
我认为字典独立于函数和/或类?
答案 0 :(得分:0)
您的代码存在一些问题。我在这里看不到全局变量的看法。
我重新格式化并重构了你的代码(除了类之外没有大写)。你的意图不明确,所以我尽我所能。函数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_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')
答案 1 :(得分:0)
让ReadFile
函数返回值
def ReadFile(line):
...
return CustomerName, CustomerItem
并在调用函数后将它们分配给变量:
for line in CustomerFile:
CustomerName, CustomerItem = ReadFile(line)
def ReadFile(line):
CustomerName = line[0:10]
CustomerItem = line[10:]
return CustomerName, CustomerItem
def Report(CustomerName, CustomerItem):
# Try to keep all print statements in one place
print 'Name : ' + CustomerName
print 'Item : ' + CustomerItem
if CustomerItem == 'Apple':
print CustomerName
else:
print CustomerName + ' is not eating an apple'
with open(CustomerFile.txt) as CustomerFile:
for line in CustomerFile:
CustomerName, CustomerItem = ReadFile(line)
Report(CustomerName, CustomerItem)
请注意,PEP8 style guide建议对变量和函数名使用lower_case,并为类保留CamelCase。虽然您可以自由使用自己的风格,但PEP8的使用非常普遍,因此通过加入PEP8俱乐部,您的代码将更自然地适用于"与其他人的代码,反之亦然。
答案 2 :(得分:0)
我找到了(明显的)答案。正如我所说,我的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