【发布时间】:2017-08-23 19:48:02
【问题描述】:
我是 Python 新手,所以仍然在处理 csv 文件。 我有两个 csv 文件:
students.csv:
name,subject1,subject2,subject3
Student1,MN1,MN2,MN3
Student2,BN1,BN2,BN3
Student3,MN4,MN5,MN6
Student4,MN2,MN3
Student5,MN7,MN1,MN2
Student6,MN8
Student7,MN1,MN2,MN3
Student8,BN4,BN5,BN1
subject.csv:
subject,VM
MN1,VM for MN1 is powering on
MN2,VM for MN2 is powering on
MN3,VM for MN3 is powering on
BN1,VM for BN1 is powering on
BN2,VM for BN2 is powering on
BN3,VM for BN3 is powering on
MN4,VM for MN4 is powering on
MN5,VM for MN5 is powering on
MN6,VM for MN5 is powering on
我的脚本需要检查学生是否注册了该科目(这部分是在我之前寻求帮助时完成的)
之后,如果学生注册了,脚本需要到第二个文件中找到学生注册的学科与VM的匹配,并输出对应的VM。
如果学生没有注册,它需要打印类似的内容(“你没有注册这个科目,请输入另一个科目”)并启动脚本开始(这部分我也有问题)..
我的代码:
#!c:/Python36/python.exe
import csv
import sys
data = {i[0]:i[1:] for i in csv.reader(open('students.csv'))}
data2 = {j[0]:j[1:] for j in csv.reader(open('subjects.csv'))}
Name = input("Please provide your name: ")
Subject = input("Please provide your Subject: ")
for data[Subject] in data2[VM]: # problem is somewhere around here I suppose
if Subject in data[Name]:
print ("you are registered - ", VM)
else:
print("you are not registered")
感谢您的帮助。
【问题讨论】: