# 查看宿舍入住信息 def view_info(): wb = openpyxl.load_workbook('c:/students.xlsx') sheet = wb.active for row in sheet.iter_rows(): for cell in row: print(cell.value, end='\t') print()用p
时间: 2024-04-27 19:21:04 浏览: 199
ython编写的程序是用来查看一个名为"students.xlsx"的Excel文件中的宿舍入住信息。程序使用了openpyxl模块来读取Excel文件,并使用了sheet.iter_rows()方法来遍历每一行数据。对于每一行数据,程序使用了两个嵌套的for循环来遍历每一个单元格,并使用print()函数将单元格的值打印出来。最终,程序将输出所有的宿舍入住信息。如果要运行此程序,请确保已经安装了openpyxl模块,并将Excel文件放置在正确的路径下。
相关问题
# 删除宿舍入住信息 def delete_info(): wb = openpyxl.load_workbook('c:/students.xlsx') sheet = wb.active name = input("请输入要删除的学生姓名:") for row in sheet.iter_rows(min_row=2): if row[0].value == name: sheet.delete_rows(row[0].row) wb.save('c:/students.xlsx') print("删除成功!") return print("未找到该学生信息!")
这段代码用于删除 Excel 文件中的学生宿舍信息。具体来说,它会提示用户输入要删除的学生姓名,并在 Excel 文件中查找该姓名对应的行。如果找到了该学生信息,它会使用 `sheet.delete_rows()` 方法删除该行,并将 Excel 文件保存。最后,它会输出“删除成功!”并返回程序执行。如果没有找到该学生信息,则会输出“未找到该学生信息!”。
需要注意的是,该代码同样使用了 openpyxl 库来操作 Excel 文件。此外,该代码假设 Excel 文件中第一行为表头,从第二行开始存储学生信息。如果实际情况不符,需要相应地修改代码。
# 修改宿舍入住信息 def edit_info(): wb = openpyxl.load_workbook('c:/students.xlsx') sheet = wb.active name = input("请输入要修改的学生姓名:") for row in sheet.iter_rows(min_row=2): if row[0].value == name: dormitory = input("请输入新的宿舍号:") row[1].value = dormitory wb.save('c:/students.xlsx') print("修改成功!") return print("未找到该学生信息!")
这段代码是用来修改 Excel 文件中学生宿舍信息的。具体来说,它会提示用户输入要修改的学生姓名,然后在 Excel 文件中查找该姓名对应的行。如果找到了该学生信息,它会提示用户输入新的宿舍号,并将该宿舍号更新到 Excel 文件中。最后,它会输出“修改成功!”并返回程序执行。如果没有找到该学生信息,则会输出“未找到该学生信息!”。
需要注意的是,该代码使用了 openpyxl 库来操作 Excel 文件,因此需要在代码中导入该库。此外,该代码假设 Excel 文件中第一行为表头,从第二行开始存储学生信息。如果实际情况不符,需要相应地修改代码。
阅读全文
相关推荐
















