Find if a value exists in a column in Excel using python

前端 未结 3 1719
萌比男神i
萌比男神i 2020-12-18 13:50

I have an Excel file with one worksheet that has sediment collection data. I am running a long Python script.

In the worksheet is a column titled “CollectionYear.”

3条回答
  •  感动是毒
    2020-12-18 14:26

    I use xlrd all the time and it works great for me. Something like this might be helpful

    from xlrd import open_workbook
    
    def main():
        book = open_workbook('example.xlsx')
        sheet = book.sheet_by_index(0)
        collection_year_col = 2 #Just an example
        test_year = 2010
        for row in range(sheet.nrows):
            if sheet.cell(row,collection_year_col).value == test_year:
                runCode()
    
    def runCode():
        #your code
    

    I hope this points you in the right direction. More help could be given if the details of your problem were known.

提交回复
热议问题