I have a xls spreadsheet that looks like below
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. So if they choose 4 it prints 1.450. This document is 10k's long so manually entering the data into python is not viable.
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. This value gets used to as the index to find the corresponding entry from Unit
.