使用XLRD模块在导入的excelfi上搜索字符串

2024-10-02 20:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大约1000行25列宽的excel文件。在

我要做的是创建一个脚本,根据用户输入查看excel文件。(本例中为姓名)

我当前的代码如下:

import xlrd
file_location =r"C:\Users\extoskben\Desktop\Datorer.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
User = input("What's the users name?:   ")

我希望代码从excel文件中读取,并根据匹配的名称打印所有列的结果。在

编辑:

excel文件的结构:

^{pr2}$

我想知道怎样才能只打印出第15行和第8行的结果,而不打印“X”。在


Tags: 文件代码用户import脚本locationexcelusers
1条回答
网友
1楼 · 发布于 2024-10-02 20:29:52

此代码执行您想要的操作:

import xlrd
file_location =r"C:\Users\extoskben\Desktop\Datorer.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
User = input("What's the users name?:   ")

for row in range(sheet.nrows):  # Iterate over every rows of the file
    if User == sheet.cell_value(row, 0):  # If the user name match the input
        for col in range(sheet.row_len(row)):  # Iterate over every columns of the cell
            print(str(sheet.cell_value(row, col)))  # Print the value of the cells.

编辑:

如果您想在第五个循环中打印第五个特定的行,则可以使用:

^{pr2}$

相关问题 更多 >