使用 openpyxl 按行读取 Python 中的数据
Row-wise data reading in Python using openpyxl
我正在尝试通过 [[a,b], [c,d],...]
格式获取 python 中的列表
例如,对于 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 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]]
我正在尝试通过 [[a,b], [c,d],...]
格式获取 python 中的列表例如,对于 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 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]]