user501590
user501590

Reputation: 1

Excel Vlookup in Python

How do I implement the Excel VLOOKUP worksheet function in Python. Any idea?

Upvotes: 0

Views: 3706

Answers (1)

John Machin
John Machin

Reputation: 82934

If you are using xlrd to read your Excel XLS file:

Get the key column values that need to be searched on:

key_values = sheet.col_values(KEY_COLX, start_rowx=START_ROWX, end_rowx=END_ROWX) # UPPER_CASE variables (KEY_COLX etc) are part of your problem description.

Search those values to find what you are looking for:

# example here is exact match
try:
    found_offset = key_values.index(QUERY_VALUE)
except IndexError:
    # not found
    # do something else

Then you pick out the data cell you want e.g.

sheet.cell(START_ROWX + found_offset, KEY_COLX + DATA_OFFSET)

Not using xlrd? See here.

Upvotes: 2

Related Questions