Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问特定的学生';什么信息?_Python_Python 3.x_Csv - Fatal编程技术网

Python 如何访问特定的学生';什么信息?

Python 如何访问特定的学生';什么信息?,python,python-3.x,csv,Python,Python 3.x,Csv,如果students.txt文件如下所示: 迈克尔·杰克逊: 公民身份证号码:102931023 性别:男 在show_student()函数中,我只知道如何检查学生是否在文件中,但我无法访问学生信息 我怎样才能访问它 代码 导入csv 导入系统 导入路径库 file_path=pathlib.path(“students.txt”) 如果文件_path.exists(): 通过 其他: 文件=打开(“students.txt”,“a+”) def login(): 用户名=输入(“用户名:”

如果
students.txt
文件如下所示:

迈克尔·杰克逊: 公民身份证号码:102931023 性别:男 在
show_student()
函数中,我只知道如何检查学生是否在文件中,但我无法访问学生信息

我怎样才能访问它

代码

导入csv
导入系统
导入路径库
file_path=pathlib.path(“students.txt”)
如果文件_path.exists():
通过
其他:
文件=打开(“students.txt”,“a+”)
def login():
用户名=输入(“用户名:”)
密码=输入(“密码:”)
如果用户名==“a”和密码==“b”:
菜单()
其他:
打印(“请重试”)
登录()
def菜单():
打印(“*”*20+“主菜单”+“*”*20)
打印(“*20+”A:注册学生“+”*20)
打印(“*20+”B:显示学生“+”*20)
打印(“*20+”C:显示特定学生“+”*20)
打印(“*20+”D:退出“+”*20)
打印(“*”*20+“主菜单”+“*”*20)
操作=输入(“输入操作:”)
如果操作.strip().lower()=“a”:
注册学生()
elif操作.strip().lower()=“b”:
向学生展示
elif操作.strip().lower()=“c”:
show_student()
elif操作.strip().lower()=“d”:
sys.exit()
其他:
打印(“无效操作”)
菜单()
def register_student():
学生id=输入(“学生id:”)
学生名=输入(“学生名:”)
学生姓=输入(“学生姓:”)
学生性别=输入(“学生性别:”)
学生人数=学生人数第一名+“”+学生人数最后一名
打开(“students.txt”,“a+”)作为学生信息:
信息=[student\u full+”:“+”\n Civil id:“+student\u Civil\u id+”\n性别:“+student\u性别]
studentInfo.write(“\n”+student\u full+”:“+”\n Civil id:“
+学生公民身份证+“\n性别:“+学生性别)
打印(“学生已注册”)
def show_students():
以open(“students.txt”)作为studentInfo:
打印(studentInfo.read())
def show_student():
学生姓名=输入(“学生姓名:”)
以open(“students.txt”)作为studentInfo:
如果studentInfo.read()中的student_name.strip().lower():
打印(“学生存在”)
其他:
打印(“学生不存在”)
登录()

我想,您在
students.txt
文件中有许多学生,如下所示:

michael jackson:
Civil id: 102931023
Gender: male
james smith:
Civil id: 165468798
Gender: male
使用此文件,您可以找到特定学生的索引,从该索引开始拆分文本,并从返回的列表中获取前3项。最后,您可以在打印时使用
join()
函数

def show_student():
    student_name = input("Student name: ")
    with open("students.txt") as f:
        text = f.read()

    student_position = text.find(student_name.strip().lower())

    if student_position != -1:
        info = "\n".join(text[student_position:].split("\n")[:3])
        print("Student information:")
        print(info)
    else:
        print("Student does not exists.")

您可以将def更改为以下内容:

 def show_student():
    student_name = input("Student name: ")
    with open("students.txt") as studentInfo:
        studentData = studentInfo.readLines()
        if len(studentData) > 0 and student_name.strip().lower() in studentData[0]:
            print(*studentData[1:])
        else:
            print("Student not exists.")

为了让您的生活更轻松,您可能需要考虑使用json、xml、csv或类似的方式创建结构化数据。你怎么知道迈克尔·杰克逊:是一个名字?

使用正则表达式匹配信息块

import re
form = r'{}:\nCivil id: \d+\nGender: [a-z]+'

def show_student():
    student_name = input("Student name: ")
    with open("students.txt") as studentInfo:
        name = student_name.strip().lower()
        pat = re.compile(form.format(name), re.MULTILINE)
        try:
            print(pat.findall(studentInfo.read())[0])
        except:
            print("Student does not exist.")

在什么意义上访问它?您希望show_student()如何工作或做什么?我希望show_student()函数检查学生姓名(取自输入),如果它在文件中,它将显示学生数据(性别和公民id)如果学生姓名不在文件中,它将打印学生不存在。OP请求
show_student()
函数,而不是
show_student()
。不客气。如果这是问题的答案,您可以给出此答案+1,然后单击接受标志。