匹配csv文件中的密钥并生成所需的输出

2024-04-26 14:50:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图操作一些代码来查看第3列中不同的uniqueclassindicator,如果第3列是2,那么查看同一uniqueclassindicator中的前一行,从该行中检索数据。另外,只有当第3列和第6列都是1时,才能实现我的输出。在

我一直在使用的代码:

from collections import defaultdict
import csv

# you probably can think up better names
fields = ('TitleA', 'TitleB', 'TitleIndicator', 'TitleRNum', 'TitleC', 'TitleD', 'TitlePNum', 'TitleBF', 'TitleCheck')

entries = defaultdict(dict)

with open("exampledata.csv", 'rb') as fd:
    reader = csv.DictReader(fd, fields)

    for counter, row in enumerate(reader):
        if counter != 0:
            TitleRNum = int(row['TitleRNum'])

            if row['TitlePNum']:
                TitlePNum = int(row['TitlePNum'])
            else:
                TitlePNum = ""

            Check = row['TitleCheck']
            Name = row['TitleB']

            key = (TitleRNum, TitleRNum)
            previous = entries[row['TitleIndicator']]

            if Check:
                # Scenario 1
                if (1, 1) in previous:
                    if (key[0] == 2 and key[1]>=2) or key[1] is None: # If Rank 2 and Position is Anything


                    if TitleRNum == 2:
                        p = previous[(2, 1)]
                        print '{p[TitleB]} {r[TitleB]} {p[TitleRNum]} {r[TitleRNum]} {p[TitlePNum]} {r[TitlePNum]} {p[TitleBF]} {r[TitleBF]} {p[TitleCheck]} {r[TitleCheck]}'.format(p=p, r=row)

            # remember this row for later rows to match against.
            previous[key] = row

示例数据:

^{pr2}$

期望输出:

Joe,Bob,1,2,1,2,984.2,994.2,Yes,Yes
Mark,Jason,1,2,1,9,295.1,,F,,

因此,为了在TitleIndicator/Uniqueclassindicator定义的每个组中阐明,如果列6和列3都等于1,我希望能够从该组的前两行中提取数据。在

如果有人能告诉我如何修复这段代码,将不胜感激。 非常感谢


Tags: csv数据key代码importifrowprevious
3条回答

你在文章的标题中使用了“键”,所以我在这里提供了一个字典的解决方案:)和哇(!)能这样做感觉很好。在

from csv import DictReader

# 1) read in the data and store it row-wise in the list 'data'
data, numclasses = [], []
with open("exampledata.csv", 'rb') as fd:
    reader = DictReader(fd)
    for counter, row in enumerate(reader):
      data.append(row)
      numclasses.append(row['TitleIndicator'][-1])
numclasses = len(list(set(numclasses))) # returns unique no. of classes

# 2) group data in a dictionary where each key uniquely corresponds to a class
datagrouped = {"class%s"%(i + 1): [] for i in range(numclasses)}
for row in data:
  classID = row['TitleIndicator'][-1]
  datagrouped["class%s"%classID].append(row)

# 3) go through each class within the dictionary, then go through the data
# within the class (row-wise), and print out rows that meet requirements.
for classname in datagrouped.keys(): # class loop
  uniq_class = datagrouped[classname]
  for i, row in enumerate(uniq_class): # row loop
    if i > 0:
      cond1 = row['TitleRNum'] == '2'
      prev_row = uniq_class[i - 1]
      cond2 = prev_row['TitleRNum'] == '1' and prev_row['TitlePNum'] == '1'
      if cond1 & cond2:
        print ["%s"%x for x in prev_row.itervalues()]
        print ["%s"%x for x in row.itervalues()]

当我在同一目录中使用exampledata.csv运行此命令时,我得到以下输出:

^{pr2}$

这是有效的:

from collections import defaultdict
import csv

# you probably can think up better names
fields = ('TitleA', 'TitleB', 'TitleIndicator', 'TitleRNum', 'TitleC', 'TitleD', 'TitlePNum', 'TitleBF', 'TitleCheck')

entries = defaultdict(dict)

with open("exampledata.csv", 'rb') as fd:
    reader = csv.DictReader(fd, fields)

    for counter, row in enumerate(reader):
        if counter != 0:
            TitleRNum = int(row['TitleRNum'])

            # If this row has a TitlePNum, keep it, otherwise reset to -1
            TitlePNum = -1
            if row['TitlePNum']:
                TitlePNum = int(row['TitlePNum'])

            # If we have already seen a row with the same class 
            # that has 1 at both RNum and PNum,
            # use that to print locally
            if row['TitleIndicator'] in entries:
                previousRow = entries[row['TitleIndicator']]
                currentRow = row

                itemsToPrint = ['TitleB', 'TitleRNum', 'TitlePNum', 'TitleBF', 'TitleCheck']
                output = ""
                for item in itemsToPrint:
                    output += previousRow[item] + ',' + currentRow[item] + ','

                # Finally, strip the last comma and print
                output = output[:-1]
                print output

                # Remove the previous entry from the dict
                del entries[row['TitleIndicator']]


            # If both RNum and PNum are 1, then save this as a candidate for future reference
            if TitleRNum == 1 and TitlePNum == 1:
                entries[row['TitleIndicator']] = row

好吧,竞争已经结束,但我还是想给出我的解决方案。以下是详细的评论答案:

# Import "csv.DictReader" and put it in the name "dr".
from csv import DictReader as dr

# These are the columns we will be working with.
cols = "TitleB", "TitleRNum", "TitlePNum", "TitleBF", "TitleCheck"

# This is a variable to hold a previous row for future processing.
# It severs the same purpose as the "entries" dict in Sudipta Chatterjee's answer.
# I set it to 0 simply so its value is false.  You could also set it to "False" or "None".
mark = 0

# Open the CSV file in binary mode.
with open("exampledata.csv", "rb") as f:

    # This loops through what is returned by "DictReader".
    #
    # The expression "f.readline().strip().split(",")" reads the first line of the file,
    # (which is the column names), strips off the newline at the end,
    # and then gets the column names by splitting the line on commas.
    for row in dr(f, f.readline().strip().split(",")):

        # This checks if "mark" is true.
        # If it is, then that means "mark" contains a previous row to be processed.
        if mark:

            # This line takes the row stored in "mark" as well as the current row
            # and puts them together, separating the values with commas using "str.join".
            print ",".join([",".join([mark[c], row[c]]) for c in cols])

        # This is a compact statement equivalent to:
        #
        #    if row["TitlePNum"] == row["TitleRNum"] == "1":
        #        mark = row
        #    else:
        #        mark = 0
        #
        # It sees if the "TitlePNum" and "TitleRNum" columns in the current row are both "1".
        # If so, it saves that row in "mark" for future processing.
        #
        # It is basically the same thing as the
        #
        #    if TitleRNum == 1 and TitlePNum == 1:
        #        entries[row['TitleIndicator']] = row
        #
        # part in Sudipta Chatterjee's answer.
        mark = row if row["TitlePNum"]==row["TitleRNum"]=="1" else 0

以下是正常情况下的答案:

^{pr2}$

输出:

Joe,Bob,1,2,1,2,984.2,994.2,Yes,Yes
Mark,Jason,1,2,1,9,395.1,,F,

如您所见,我的解决方案更小,效率更高。在

相关问题 更多 >