У меня есть файл excel размером около 1000 строк и шириной 25 колонок.
Я хочу создать скрипт, который просматривает 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 the users name?: ")
Я хочу, чтобы код читался из excel файла и печатал результат из всех столбцов на основе имени, которое оно соответствует.
РЕДАКТИРОВАТЬ:
Структура файла excel:
[Name ] [x] [x] [x] [Serialnumber] [x] [x] [ Model ]
[1] Oskar Beneke x x x 123456789 x x Thinkpad t470
[2] Example name x x x 987654321 x x Thinkpad t470s
Я хотел бы знать, как я могу распечатать результаты только из строк 1 5 и 8 и оставить "X" из моей печати.
Этот код делает то, что вы хотите:
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 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.
РЕДАКТИРОВАТЬ :
Если вы хотите напечатать только определенные столбцы (первый, пятый и третий в этом примере) данных строк, вы можете использовать эту версию цикла for:
columns_to_output = [0, 4, 7] # List of columns to ouptput (starting with '0' for first column index)
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 columns_to_output: # Iterate over the column to output list
print(str(sheet.cell_value(row, col))) # Printing the cell at given column in the row.