简体   繁体   English

如何将csv文件的行转换为对象列表?

[英]How can I turn the rows of a csv file into a list of objects?

I am working with patient data. 我正在处理患者数据。 I am storing the patient data in a CSV file. 我将患者数据存储在CSV文件中。 Each row is a new patient and there are hundreds of patients. 每行是一个新患者,有数百名患者。 I would like to access patients as incidences of a list. 我想以清单的发生率来访问患者。 For example, index 0 of a my patients list would output a patient object with attributes like: 例如,我的患者列表的索引0将输出具有以下属性的患者对象:

{ first: 'Zakariah', last: 'Siyaji', age: 21 } {首先:“撒迦利亚”,最后:“ Siyaji”,年龄:21}

and so on. 等等。 The following is the code that I was writing although I couldn't see how I could generate many objects without having the initialize each one manually. 以下是我正在编写的代码,尽管我看不到如何在没有手动初始化每个对象的情况下生成许多对象。 Considering that there are hundreds of patients, this would be highly impractical. 考虑到有数百名患者,这是非常不切实际的。

import ASD as asd

class Patient:
    mrn = 0
    first = '' 
    last = '' 
    gender = 0
    smoker = 0
    bmi = 0
    asa = 0 
    cci = 0
    dob = '' 
    dos = '' 
    age = 0

    def setData(self, mrn, first, last, gender, smoker, bmi, asa, cci, dob, dos, age):
        self.mrn = mrn
        self.first = first
        self.last = last
        self.gender = gender
        self.smoker = smoker
        self.bmi = bmi
        self.asa = asa
        self.cci = cci
        self.dob = dob
        self.dos = dos
        self.age = age

    def showData(self):
        print("MRN\t:",self.mrn)
        print("First\t:", self.first)
        print("Last\t:", self.last)
        print("Gender\t:", self.gender)
        print("Smoker\t:", self.smoker)
        print("BMI\t:",self.bmi)
        print("ASA\t:", self.asa)
        print("CCI\t:", self.cci)
        print("DOB\t:", self.dob)
        print("DOS\t:", self.dos)
        print("Age\t:", self.age)

def main():

    p1 = Patient()
    p1.setData(asd.file1['MRN'][0],
               asd.file1['First'][0],
               asd.file1['Last'][0],
               asd.file1['Female'][0],
               asd.file1['Smoker'][0],
               asd.file1['BMI'][0],
               asd.file1['ASA'][0],
               asd.file1['CCI'][0],
               asd.file1['DOB'][0],
               asd.file1['DOS'][0],
               asd.file1['Age'][0])
    p1.showData()

if __name__ == '__main__':
    main()

You can simply import pandas and read your CSV file and access the information of patients. 您只需导入熊猫并读取CSV文件并访问患者信息即可。

import pandas as pd
patients = pd.read_csv("patients.csv")

# To access information
patients.loc[0] #or patients.iloc[0]

The csv module supports reading from a csv file. csv模块支持从csv文件读取。 If you wish to do complex data interpolation then ofcourse there's pandas. 如果您想进行复杂的数据插值,那么当然还有熊猫。 Assuming its a simple operation this should look something like this, 假设它是一个简单的操作,它应该看起来像这样,

import csv

with open(filename, 'r') as stream:
  reader = csv.DictReader(stream)
  count == 0
  for row in reader:
    if count != 0 # skip the header, or do something here
    p = Patient(row["mrn"], row["first"], row["last"], row["gender"], row["smoker"], row["bmi"], row["asa"], row["cci"], row["dob"], row["dos"], row["age"])
    print(p)
    count += 1

Also I see your patient definition class doesn't follow idiomatic python. 我也看到您的患者定义类不遵循惯用的python。 It can be rewritten to be more pythonic in the following way. 可以通过以下方式将其重写为更加Pythonic的形式。

class Patient:
   def __init___(self, mrn=None, first=None, last=None, gender=None, smoker=None, bmi=None, asa=None, cci=None, dob=None, dos=None, age=None)
     self.mrn = mrn
     self.first = first
     self.last = last
     self.gender = gender
     self.smoker = smoker
     self.bmi = bmi
     self.asa = asa
     self.cci = cci
     self.dob = dob
     self.dos = dos
     self.age = age
def __repr__(self):
    return '''MRN: {}
            First: {}
            ...
            ASA: {}'''.format(self.mrn, self.first,....,self.asa)

Since your constructor has many fields, one of the biggest advantages is that you can use keyword arguments to instantiate your class and run a lower risk of messing some field with some other value. 由于构造函数具有许多字段,因此最大的优点之一就是可以使用关键字参数实例化您的类,并降低将某些字段与其他值混淆的风险。

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

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