[英]Row-wise data reading in Python using openpyxl
I am trying to obtain a list in python by the format [[a,b], [c,d],...]我正在尝试以 [[a,b], [c,d],...] 格式获取 python 中的列表
As an example, for two columns below in excel:例如,对于 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]]我应该获得一个列表 = [[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!谢谢!
I am using xlrd package to read the excel worksheets我正在使用 xlrd package 来阅读 excel 工作表
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.