使用openpyxl在Python中读取行数据
我试图以python的格式[[a,b],[c,d],…]获取一个列表 例如,对于excel中的以下两列:使用openpyxl在Python中读取行数据,python,python-3.x,list,openpyxl,Python,Python 3.x,List,Openpyxl,我试图以python的格式[[a,b],[c,d],…]获取一个列表 例如,对于excel中的以下两列: R1 R2 1 2 6 2 0 9 3 2 4 1 5 6 我应该得到一个列表=[[1,2],[6,2],[0,9],[3,2],[4,1],[5,6]] 我的代码如下所示: R = [] # iterate over column for col in r
R1 R2
1 2
6 2
0 9
3 2
4 1
5 6
我应该得到一个列表=[[1,2],[6,2],[0,9],[3,2],[4,1],[5,6]]
我的代码如下所示:
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}')
但结果给了我这样的东西:
R = [[1, 6, 0, 3, 4, 5], [2, 2, 9, 2, 1, 6]]
有没有办法改变这个?任何帮助都将不胜感激!谢谢 我正在使用xlrd软件包阅读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]]
非常感谢。我在使用openpyxl时得到了它。我认为与我的第一个代码不同的是,我使用循环遍历行之前的列。这就是为什么读取的数据是按列的。非常感谢你!!