比较Python中的两个csv文件

时间:2017-08-23 11:10:38

标签: python csv

我是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。

如果学生没有注册,则需要打印类似的内容(“您未注册此主题,请输入其他主题”)并启动脚本(此部分我也遇到问题).. < / p>

我的代码:

#!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")

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

根据我的理解,您需要从第二个文件中查找主题并打印相应的主题行。如果我理解正确,那么你实际上不需要遍历data2,而是使用无限循环。如下所示:

i = 0
while i == 0:
    Name = input("Please provide your name (q to quit): ")
    if Name == 'q':
        i = 1
    else:
        Subject = input("Please provide your Subject: ")
        if Subject in data[Name]:
            if Subject in data2.keys():
                print ("you are registered - ", data2[Subject])
            else:
                print("The VM for the subject doesn't exist")
        else:
            print("you are not registered")

你的初始代码有缩进错误,但我认为这是无意的:)而且它正在寻找变量VM,你没有在任何地方定义。另一件事是,如果您使用csv.reader读取csv文件,您的数据将有一个额外的键与表标题。像这样:

数据:

{'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'],
 'name': ['subject1', 'subject2', 'subject3']}

DATA2:

{'BN1': ['VM for BN1 is powering on'],
 'BN2': ['VM for BN2 is powering on'],
 'BN3': ['VM for BN3 is powering on'],
 'MN1': ['VM for MN1 is powering on'],
 'MN2': ['VM for MN2 is powering on'],
 'MN3': ['VM for MN3 is powering on'],
 'MN4': ['VM for MN4 is powering on'],
 'MN5': ['VM for MN5 is powering on'],
 'MN6': ['VM for MN5 is powering on'],
 'subject': ['VM']}

使用以下内容删除额外条目:

del data['name']
del data2['subject']

另外,我强烈建议您查看Python Pandas库,它应该处理这些琐碎的事情。现在看起来似乎并不重要,但是当您处理大量数据时,内置的pandas优化将会派上用场。此外,您可以使用库进行更多操作,并且最好拥有使用库的经验,每个人都可以使用这些库来处理csv和数据。