【问题标题】:Comparing two csv files in Python在 Python 中比较两个 csv 文件
【发布时间】: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")

感谢您的帮助。

【问题讨论】:

    标签: python csv


    【解决方案1】:

    据我了解,您需要从第二个文件中查找主题并打印主题的相应行。如果我理解正确,那么您实际上不需要遍历 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']}
    

    数据2:

    {'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 和一般数据的库的经验是个好主意。

    【讨论】:

    • 非常感谢您的回答,伙计。对此,我真的非常感激。你能解释一下“data2.keys()”是从哪里来的吗?其实我刚开始看熊猫,谢谢你的建议。顺便说一句,你知道 php 中是否有类似的东西,因为我想通过 php 执行这个脚本,但我想我的免费虚拟主机不允许我这样做,所以我可能需要用 php 重写它?
    • "data2.keys()" 获取字典中的所有键。我用它来查看学生注册的主题是否在主题文件中有条目。主题 MN8 在第二个 csv 中没有条目,因此它是一个保护代码。您可以在 python 文档上阅读有关 python 字典的信息。至于php,对不起,我不懂php,所以帮不上忙。另外,如果答案对您有用,请将其标记为答案并点赞,以便将来的搜索者知道哪个是正确的解决方案:)
    • 谢谢。我还有一个问题 - 如果在 subject.csv 中有 Web 链接,而不仅仅是文本说 VM for ...,是否可以运行此链接而不是打印文本...我尝试了几种方法,但它说TypeError: startfile: filepath 应该是字符串、字节或 os.PathLike,而不是 NoneType...有什么想法吗?
    • 你必须做一些解析,我猜。您可以使用正则表达式检查它是否为 http 格式并使用 webbrowser 库打开它。谷歌搜索检查网络链接会给你很多好的结果 :) 至于你得到的错误,如果没有看到实际的代码或数据就很难判断。据我所知,您正在尝试传递一个空变量(至少 NoneType 在那个特定时间弹出)
    • 谢谢伙计,我通过正则表达式使它工作;)终于完成了我的算法。最后一个挑战是在 php 中运行它..
    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2014-08-24
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多