Search code examples
pythonlistloopsxlrd

Compare lists in python while looping


I have a script which I'm using to read an excel file and update an SQL database. I'm reading the excel file every 30 seconds using a loop. However I only want to update the database when the excel file changes

If I use the != operator when the loop cycles it refreshes the value of 'temp' and thus does not register that the value is the same.

Does anyone have an idea how to solve this problem..?

Thanks! edit: updated to make my problem more clear!

def update(): 
    threading.Timer(1, update).start()
    book = open_workbook('bet.xls')


    def odds():
        sheet = book.sheet_by_name('xyz')
        match_sheet = sheet.cell(5,0).value  
        data = book.sheet_by_name(sheet)
        vv = data.cell(3,26).value

        temp= None 

        if vv != temp:
            print 'hello'

        temp= vv

odds()

update()


Solution

  • Yes, Python built-in containers are compared by value (both tuples, lists and dicts).

    Something like this (I used a list comprehension to add fanciness):

    //init
    pvv=None
    
    <...>
    
    //iteration
    vv= [data.cell(i,j).value for (i,j) in ((2,26),(3,26),(4,26))]
    if vv!=pvv: 
        //do something
        pvv=vv