How do I implement the Excel VLOOKUP workshe开发者_高级运维et function in Python. Any idea?
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.
精彩评论