Hi I was wondering if it is possible to process 2 different files with one function and store the strings/lines I'm searching for in different variables as for instance number1 for file 1 and number 2 for file 2.

the code so far:

import string

Name = raw_input("Filename ")
infile = open(Name, 'r')
data = infile.readlines()

def getdata(data):
   listing = []
   for line in data:
       new_line = string.split(line)[:2]
       listing.append(new_line)
  
   number = len(listing)
   items = listing

def main():
   number, items = getdata(data)

The problem is that I want to get number1 and itmes1 or number 2 and items2 if data = data1 or data = data2 and another problem is that I get an error message:
TypeError: 'NoneType' object is not iterable
when I call main()

Thanks for ideas,
awa

Recommended Answers

All 9 Replies

first of all your function getdata should return the values you want :

def getdata(data):
    # the code you've already written
    return number, items

On top of jice's comment, you don't need to import string, and it would benefit you to move your top-level code into your main function:

def getdata(data):
    listing = []
    for line in data:
        new_line = line.split()[:2]
        listing.append(new_line)
  
    number = len(listing)
    items = listing

    # This is what jice added
    return number, items

def main():
    Name = raw_input("Filename ")
    infile = open(Name, 'r')
    data = infile.readlines()
    number, items = getdata(data)

''' This is a pretty common practice in Python to call your main
function (this wont get called if another script imports this
a module, as the other script will in that case be __main__,
not this one):
'''
if __name__ == '__main__':
    ''' If you want your program to loop you would put that
       logic here; for instance :
    while 1:
        main()
    '''
    main()

import string and things like
new_line = string.split(line)[:2]
have been deprecated a few Python versions ago, as jlm699 pointed out, you now simply use:
new_line = line.split()[:2]

Is it possible to create a global variable out of a local variable from inside the function?

def getdata(data, numberX, itemsX):
     listing = []
     for line in data:
          new_line = line.split()[:2]
          listing.append(new_line)

     global numberX
     numberX = len(listing)
     global itemsX
     itemsX = listing
 
def main1():
     Name = raw_input("Filename 1 ")
     infile = open(Name, 'r')
     data = infile.readlines()
     getdata(data, number1, items1)  # replace numberX in getdata with number1, ect.

def main1():
     Name2 = raw_input("Filename 2 ")
     infile2 = open(Name2, 'r')
     data2 = infile2.readlines()
     getdata(data2, number2, items2)

The problem is that I want to call the same function twice so I get in the end number1 from "Filename 1" and number2 from "Filename 2" etc. as global variables.
How can I avoid the error: "numberX is local and global")

Thanks for ideas,
awa

The use of global variables is a BAD IDEA.

Ok do you have other suggestions?

Ok do you have other suggestions?

Instead of making your variables global, simply return them from the function. Here's an example of a function with return values

>>> def retVals(inp):
...     inp += 5
...     out = inp*3
...     return inp, out
...     
>>> a = 4
>>> b, c = retVals(a)
>>> print a,b,c
4 9 27
>>> a = 10
>>> b, c = retVals(a)
>>> print a,b,c
10 15 45
>>>

My suggestion was this one. And the same the others made.
If you simply had added this line I suggested, your program would have worked. Without any other change.
Functions are to return values (one or more) and this is to be used.

first of all your function getdata should return the values you want :

def getdata(data):
    # the code you've already written
    return number, items

workes of course just had a problem with nested functions. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.