简体   繁体   English

Vlookup XLRD Python

[英]Vlookup XLRD Python

I have a xls spreadsheet that looks like below 我有一个xls电子表格,如下所示

Number  Code         Unit
1       Widget 1     20.0000 
2       Widget 2     4.6000 
3       Widget 3     2.6000 
4       Widget 4     1.4500 

I have created the following code: 我创建了以下代码:

import xlrd
wb=xlrd.open_workbook('pytest.xls')
xlsname = 'pytest.xls'
book = xlrd.open_workbook(xlsname)

sd={}
for s in book.sheets():
    sd[s.name] = s
sheet=sd["Prod"]

Number = sh.col_values(0)
Code = sh.col_values(1)
Unit = sh.col_values(2)

Now this is where I am getting stuck, what i need to do is ask a question on what Number they choose, for this example lets say they choose 3, it needs to do print the answer for the unit. 现在这是我遇到的问题,我需要做的是问一个关于他们选择的数字的问题,对于这个例子,假设他们选择3,它需要打印单位的答案。 So if they choose 4 it prints 1.450. 因此,如果他们选择4,则打印1.450。 This document is 10k's long so manually entering the data into python is not viable. 该文档的长度为10k,因此无法手动将数据输入python。

In this case you'd just need to do this: 在这种情况下,您只需要执行以下操作:

 Unit[Number.index(value)]

Which will return the value from the Unit column that corresponds to the value specified for the Number column. 它将从“单位”列返回与“数字”列指定的值相对应的值。

The index() function on a Python sequence returns the index of the first occurence of the provided value in the sequence. Python序列上的index()函数返回序列中提供的值首次出现的索引。 This value gets used to as the index to find the corresponding entry from Unit . 该值将用作索引,以从Unit找到相应的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM