繁体   English   中英

如何使用csv文件检查用户输入并从特定列打印数据?

[英]How to check user input with csv file and print data from specific column?

我有一个CSV文件,该文件在单独的列中包含补丁名称,发布日期和其他一些信息。 我正在尝试编写一个Python脚本,该脚本将要求用户输入补丁名称,一旦获得输入,便会检查补丁是否在CSV文件中并打印出发布日期。

到目前为止,我写了下面的一段代码,它基于基于我这个答案。

import csv

patch = raw_input("Please provide your Patchname: ")

with open("CSV_File1.csv") as my_file1:
    reader = csv.DictReader(my_file1)
    for row in reader:
        for k in row:
            if row[k] == patch:
                print "According to the CSV_File1 database: "+row[k]

通过这种方式,我将补丁名称打印在屏幕上。 我不知道如何使用“日期”遍历该列,以便我可以打印与输入的Patch名称对应的日期。

另外,我想检查该补丁程序是否是最新发布的补丁程序。 如果不是,则打印最新版本及其发行日期。 我的问题是CSV文件包含不同软件版本的补丁程序名称,因此我不能仅打印列表的最后一个。 例如:

PatchXXXYY,...other columns...,Release Date,...     <--- (this is the header row of the CSV file)
Patch10000,...,date
Patch10001,...,date
Patch10002,...,date
Patch10100,...,date
Patch10101,...,date
Patch10102,...,date
Patch10103,...,date
Patch20000,...,date
...

因此,如果我输入的是“ Patch10000”,那么我应该得到它的发布日期和最新的可用Patch(在这种情况下为Patch10002)及其发布日期。 但是不是Patch20000,因为那是不同的软件版本。 更好的输出是这样的:

根据CSV_File1数据库:Patch10100已于“日期”发布。 最新的可用补丁是“ Patch10103”,该补丁已于“ date”发布。

这是因为上面的补丁XXXYY中的“ XXX”数字代表软件版本,而“ YY”则是补丁编号。 我希望这很清楚。

提前致谢!

你几乎没有,虽然我一丁点儿的困惑-你的样本数据不具有标题行。 如果没有,那么您不应该使用DictReader但是如果使用了DictReader ,则可以采用这种方法。

version = patch[:8]
latest_patch = ''
last_patch_data = None
with open("CSV_File1.csv") as my_file1:
    reader = csv.DictReader(my_file1)
    for row in reader:
        # This works because of ASCII ordering. First,
        # we make sure the package starts with the right
        # version - e.g. Patch200
        if row['Package'].startswith(version):
            # Now we grab the next two numbers, so from
            # Patch20042 we're grabbing '42'
            patch_number = row['Package'][8:10]
            # '02' > '' is true, and '42' > '02' is also True
            if patch_number > latest_patch:
                # If we have a greater patch number, we
                # want to store that, along with the row that
                # had that. We could just store the patch & date
                # but it's fine to store the whole row
                latest_patch = patch_number
                last_patch_data = row

        # No need to iterate over the keys, you *know* the
        # column containing the patch. Presumably it's
        # titled 'patch'
        #for k in row:
        #    if row[k] == patch:
        if row['Package'] == patch:
            # assuming the date header is 'date'
            print("According to the CSV_File1 database: {patch!r}"
                  " was released on {date!r}".format(patch=row['Package'],
                                                     date=row['Registration']))

    # `None` is a singleton, which means that we can use `is`,
    # rather than `==`. If we didn't even *start* with the same
    # version, there was certainly no patch. You may prefer a
    # different message, of course.
    if last_patch_data is None:
        print('No patch found')
    else:
        print('The latest available patch is {patch!r},'
              ' which was released on {date!r}'.format(patch=last_patch_data['Package'],
                                                       date=last_patch_data['Registration']))

CSV模块工作正常,但我只想将Pandas放入其中,因为这可能是一个很好的用例。 可能有更好的方法来解决此问题,但这是一个有趣的示例。 这是假设您的列是标签(Patch_Name,Release_Date),因此您需要对其进行更正。

import pandas as pd

my_file1 = pd.read_csv("CSV_File1.csv", error_bad_lines=False)

patch = raw_input("Please provide your Patchname: ")

#Find row that matches patch and store the index as idx
idx = my_file1[my_file1["Patch_Name"] == patch].index.tolist()

#Get the date value from row by index number
date = my_file1.get_value(idx[0], "Release_Date")

print "According to the CSV_File1 database: {} {}".format(patch, date)

还有很好的方法来过滤和比较CSV与Pandas中的数据。 如果我有更多时间,我会提供更多描述性的解决方案。 我强烈建议您查阅Pandas文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM