Reputation: 109
I am trying to obtain a list in python by the format [[a,b], [c,d],...]
As an example, for two columns below in excel:
R1 R2
1 2
6 2
0 9
3 2
4 1
5 6
I should obtain a list = [[1,2], [6,2], [0, 9], [3,2], [4,1], [5,6]]
The code I have is shown below:
R = []
# iterate over column
for col in range(min_column+3, max_column+1): #location of the data
# u_need reads in the resource needed for each activity
u_need = []
# iterate over row
for row in range(2, max_row+1): #row 1 is not included
# loop until blank space is encountered
if (sheet.cell(row,col).value is not None):
u_need.append(sheet.cell(row,col).value);
R.append(u_need)
print(f'R = {R}')
but the result gives me something like this:
R = [[1, 6, 0, 3, 4, 5], [2, 2, 9, 2, 1, 6]]
Is there a way I could change this? Any help would be appreciated! Thanks!
Upvotes: 0
Views: 1471
Reputation: 879
I am using xlrd package to read the excel worksheets
import xlrd
# my own excel sheet location which contains your data
loc = "a.xlsx"
# opening workbook
wb = xlrd.open_workbook(loc)
# selecting the first worksheet
sheet = wb.sheet_by_index(0)
lst = []
# not including the first row
for i in range(1,sheet.nrows):
dummy = []
for j in range(sheet.ncols):
# appending the columns with the same row to dummy list
dummy.append(sheet.cell_value(i, j))
# appending the dummy list to the main list
lst.append(dummy)
print(f"lst = {lst}") # output lst = [[1.0, 2.0], [6.0, 2.0], [0.0, 9.0], [3.0, 2.0], [4.0, 1.0], [5.0, 6.0]]
Upvotes: 1