将CSV记录解析到类列表中 [英] Parse CSV records into a list of Classes

查看:85
本文介绍了将CSV记录解析到类列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow的第一篇文章,虽然我已经读了几年了!



我使用Python来清理和分析CSV数据转储IV曲线。



我的基本问题是数据记录器给我的格式:每隔几分钟,在单个实例中,它需要大约100次电压测量),Current(i)和Power(p),并将它们转储为CSV文件。下一个测量附加到此。所以,我们得到的数据结构是:

  Date1; 0.2; 0.1; 0.02 
Date1; 0.3 ; 0.1; 0.03
Date1; 0.4; 0.1; 0.04
Date2; 0.2; 0.1; 0.02
Date2; 0.3; 0.1; 0.03
Date2; 0.4; 0.1; 0.04
Date3; etc ...

此数据存储在文件Data.csv



我写了一个名为IVCurve的类:

  class IVCurve:
def __init__ (self,datetime):
self.datetime = datetime
v = []
i = []
p = []

并且我想创建这些类实例的列表为:

  count = -1 
thelist = []
prev_time = 0

import csv

with open('Data.csv','rb ')as IVdump:
IVdata = csv.reader(IVdump,delimiter =';')
对于datata,v,i,p在IVdata中:
# date
if prev_time!= datetime:
#注意新记录
prev_time = datetime
#go到列表中的下一个位置[]
count + = 1
#create a new curve
thelist.append(IVCurve(datetime))
#在任何情况下,将v和i的值放入此列表[count]
thelist [计数] .v.append(float(v))
thelist [count] .i.append(float(i))
thelist [count] .p.append(float(v)* float i))

我遇到的问题是v和i的所有值都放在每个列表[]的实例,即,我得到一个IVCurve实例列表,每个实例具有不同的datetime,但每个实例具有相同的v,i和p集合(该集合表示所有日期组合的整个数据集)。



我不明白我在这里做错了什么。对我来说,每次计数都增加(每次我们找到一个新的日期),列表[count]应该是一个新的,唯一的记录。



示例数据我使用了这段代码)我已经粘贴在这里: http://pastebin.com/Dw4dd7tu

所以问题是:如何区分这些值?



感谢您的帮助!

解决方案

问题是你有

  class IVCurve:
def __init __(self,datetime):
self.datetime = datetime
v = []
i = []
p = []

这些列表是类上的列表。所以每个类的实例都有相同的列表。您希望每个实例的列表不同,因此您应该在 __ init __ 方法中创建它们。

  class IVCurve:
def __init __(self,datetime):
self.datetime = datetime
self.v = []
self.i = []
self.p = []


My first post on StackOverflow, though I've been a reader for a few years now!

I'm using Python to sanitize and analyse CSV data dumps of IV Curves.

My basic problem is the format that the datalogger gives me: every few minutes, at a single instance in time, it takes about 100 measurements of Voltage (v), Current (i), and Power (p), and dumps them into a CSV file. The next measurement gets appended to this. So, the structure of the data that we get is:

Date1;0.2;0.1;0.02
Date1;0.3;0.1;0.03
Date1;0.4;0.1;0.04
Date2;0.2;0.1;0.02
Date2;0.3;0.1;0.03
Date2;0.4;0.1;0.04
Date3; etc...

This data is stored in a file Data.csv

I've written a class called IVCurve:

class IVCurve:
    def __init__(self, datetime):
        self.datetime = datetime
    v = []
    i = []
    p = []

and I want to create a list of these class instances as:

count = -1
thelist = []
prev_time = 0

import csv

with open('Data.csv', 'rb') as IVdump:
    IVdata = csv.reader(IVdump, delimiter=';')
    for datetime, v, i, p in IVdata:
        # if we're onto a new date
        if prev_time != datetime:
            # note the new record
            prev_time=datetime
            #go to the next position in thelist[]
            count +=1
            #create a new curve
            thelist.append(IVCurve(datetime))
        # in any case, put the values of v, and i into this thelist[count]
        thelist[count].v.append(float(v))
        thelist[count].i.append(float(i))
        thelist[count].p.append(float(v)*float(i))

The problem I'm having is that all the values of v and i are placed in EVERY instance of thelist[], i.e., I'm getting a list of IVCurve instances, each with a different datetime, but each with the same set of v, i, and p (and that set represents the entire dataset for all dates combined).

I don't understand what I've done wrong here. Seems to me that every time count is incremented (each time we find a new date), thelist[count] should be a new, unique record.

Sample data (which I've used with this code) I've pasted here: http://pastebin.com/Dw4dd7tu

So the question is: how can I separate the values?

Thanks alot for your help!

解决方案

The problem is that you have

class IVCurve:
    def __init__(self, datetime):
        self.datetime = datetime
    v = []
    i = []
    p = []

These lists are then lists on the class. So every instance of the class has the same list. You want the lists to be different for each instance, so you should create them in your __init__ method.

class IVCurve:
    def __init__(self, datetime):
        self.datetime = datetime
        self.v = []
        self.i = []
        self.p = []

这篇关于将CSV记录解析到类列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆