我试图在名为 Student.txt 的文件中拆分 studentID 和 studentName,这样我就可以让用户输入以在文件中搜索特定的学生 ID 并显示学生姓名和 ID。但是我不知道如何将文件中的学生ID和学生姓名分开。
这是我的文件内容
201707001 Michael_Tan
201707002 Richard_Lee_Wai_Yong
201707003 Jean_Yip
201707004 Mark_Lee
201707005 Linda_Wong
201707006 Karen_Tan
201707007 James_Bond
201707008 Sandra_Smith
201707009 Paul_Garcia
201707010 Donald_Lim
这是我在 Python 中尝试的代码
# user can search the studentID
searchStudent = input("Please enter a student ID: ")
# read the students file
with open('C:\\Users\\jaspe\\Desktop\\PADS Assignment\\Student.txt') as f:
studentFile = f.readlines()
for student in studentFile:
print(student)
请您参考如下方法:
您可以使用student.split("")将每一行拆分为id和name
searchStudent = input("Please enter a student ID: ")
with open('C:\\Users\\jaspe\\Desktop\\PADS Assignment\\Student.txt') as f:
studentFile = f.readlines()
for student in studentFile:
id, name = student.strip().split(" ", 1)
