简体   繁体   English

跳过从 python 中的 CSV 文件读取一次的观察

[英]Skip observations that been read once from a CSV file in python

i read a CSV file with few columns and created a class for those columns:我阅读了一个包含几列的 CSV 文件,并为这些列创建了一个 class:

  class member:
    name = ""
    vdate = []

data = []
for column in columns:
    m = member()
    m.name = column[1] + " " + column[2]
    m.date = int(column[5]

Now, i'm trying to skip lines that have been read once from m.name, the skiped lines wont be append to现在,我正在尝试跳过从 m.name 读取一次的行,跳过的行不会是 append 到

data = []数据 = []

However, after I skiped those lines, I want the date value in the skip lines to be append to the vdate [] in the class.但是,在我跳过这些行之后,我希望跳过行中的日期值是 append 到 class 中的 vdate []。

I tried:我试过了:

  if m.name != m.name:
    data.append(m)
else:
    continue and vdate.append(m)

did not worked.没用。

I do not think that a Class is the best way to go about this.我不认为 Class 是 go 的最佳方式。

I would just have a simple dict to hold the name as a key and the date as a value.我只需要一个简单的字典来保存名称作为键和日期作为值。 And then just check if each new line is in the dict.然后只需检查每个新行是否在字典中。 If it is not add it to the dict, and if it is just the date to a another list.如果不是将其添加到字典中,并且它只是另一个列表的日期。

So that should give you a dict with key value pairs of names and dates, and a list with the dates where the name is already in the dict.所以这应该给你一个包含名称和日期的键值对的字典,以及一个包含名称已经在字典中的日期的列表。

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

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