![](/img/trans.png)
[英]Openpyxl - How to read only one column from Excel file in Python?
[英]openpyxl to read from Excel file
我有一些代碼,我想用它來遍歷 excel 中的一些行,為此我使用 openpyxl。
我想為每一行打印如下內容:
'The product id is: ' + column 1 + 'and the product is: ' + column 2
但我不知道如何區分代碼中的列。 我現在擁有的代碼如下:
from openpyxl import load_workbook
file = load_workbook('path...file.xlsx')
list = file.active
for value in list.iter_rows(min_row=1, min_col=1, max_col=2, values_only=True):
print(value)
我得到的結果如下:
('ProductID', 'Product')
('xxx', 'xxx')
...
('yyy', 'yyy')
所以我可以使用整行,但我不確定在打印時應該如何引用行中的每個單獨的單元格。
我應該這樣做嗎?
請試試這個:
from openpyxl import load_workbook
file = load_workbook('path...file.xlsx')
sheet = file.active
for row in range(sheet.max_row):
cell = sheet.cell(row=row, column=1)
print(cell.value)
您可以打開文件並使用 readlines() 然后遍歷行並使用逗號分隔值以列出並使用例如 value[0] 作為 id 和 value[1] 作為產品
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.