[英]Using openpyxl to find a value in a particular column and assign each value of the row to its own variable
我是 openpyxl 的新手,所以如果有错误我没有注意到。 我尝试过该程序在 excel 表的列中查找名称,如果找到该名称,则应将行中的每个值分配给一个变量。 搜索名称有效,但我不知道如何将各个值分配给变量。 下面的代码是一个例子。
代码:
import openpyxl
file = r"C:\Users\user\Documents\info.xlsx"
excel_file = openpyxl.load_workbook(file, read_only=True)
excel_sheet = excel_file["sheet2"]
user_input = input("Name: ")
for row in excel_sheet.rows:
if user_input in row[0].value:
for cell in row:
print(cell.value)
例子:
https://i.stack.imgur.com/Wv8oU.png
name1 = Markus,age1 = 40,country1 = UK...
是不是这么简单:
file = r"C:\Users\user\Documents\info.xlsx"
excel_file = openpyxl.load_workbook(file, read_only=True)
excel_sheet = excel_file["sheet2"]
user_input = input("Name: ")
name = None
age = None
country = None
for row in excel_sheet.rows:
if user_input in row[0].value:
name = row[0].value
age = row[1].value
country = row[2].value
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.