Evangelos Con
Evangelos Con

Reputation: 516

Python: Trying to import a loop

I am new to programming with Python and I have some difficulties with importing a loop I created in order to manipulate some data.

Specifically, in my main script I have the following code:

wb=xw.Workbook('/Census/B05.xls')

numbers=range(16,56)

multiples8=range(16,56,8)

data=list(set(numbers)-set(multiples8))

import loop1

After I run the command import loop1, I get the error:

  File "<ipython-input-15-d5d478cf704f>", line 10, in <module>
    import loop1

  File "loop1.py", line 8, in <module>
    for x in set(data):

NameError: name 'data' is not defined.

The loop1 script reads:

for x in set(data):

        if x-1 in set(multiples8): xw.Range('SQL Results','L'+str(x-1)+':S'+str(x-1)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value

          else:

       if x-2 in set(multiples8): xw.Range('SQL Results','T'+str(x-2)+':AA'+str(x-2)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value

         else:
            if x-3 in set(multiples8):
                    xw.Range('SQL Results','AB'+str(x-3)+':AI'+str(x-3)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value
            else:
                if x-4 in set(multiples8):
                        xw.Range('SQL Results','AJ'+str(x-4)+':AQ'+str(x-4)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value
                else:
                    if x-5 in set(multiples8):
                            xw.Range('SQL Results','AR'+str(x-5)+':AY'+str(x-5)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value
                    else:
                        if x-6 in set(multiples8):
                                xw.Range('SQL Results','AZ'+str(x-6)+':BG'+str(x-6)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value
                        else:
                            if x-7 in set(multiples8):
                                    xw.Range('SQL Results','BH'+str(x-7)+':BO'+str(x-7)).value=xw.Range('SQL Results','D'+str(x)+':K'+str(x), atleast_2d=True).value

I would greatly appreciate any help.

Thanks.

Upvotes: 1

Views: 190

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191963

Try to make a method in loop1.py that accepts data as a parameter, then import that.

# loop1.py
def my_loop(data):
    for x in set(data):
        # stuff

Then the rest of the code would read

wb=xw.Workbook('/Census/B05.xls')

numbers=range(16,56)
multiples8=range(16,56,8)
data=list(set(numbers)-set(multiples8))

from loop1 import my_loop
my_loop(data)

Upvotes: 2

Related Questions