来自两个来源的Python构建对象

时间:2018-10-12 09:17:10

标签: python

我需要能够使用从csv文件列中提取的数据来构建我的buildObject

class BuildObject(ObjectID):

    def __init__(self, ObjectID, ObjectName, ObjectPrice, ObjectLocation, ObjectColour, ObjectAge, ObjectTag):
        self.ObjectID= ObjectID
        self.ObjectName= ObjectName


    def main():
         with open(filename1, "r") as csv1, open(filename2, "r") as csv2:  
            csvReader1 = csv.DictReader(csv1)
            csvReader2 = csv.DictReader(csv2)

            csvList = []
            for row1, row2 in zip(csvReader1, csvReader2):  
                csvList.append((row2["ObjectName"], row1["ObjectId"], row1["ObjectPrice"]))

            return csvList

1 个答案:

答案 0 :(得分:0)

  

评论:我担心的是,如果csv文件具有完全相同的objectID和相同的顺序,它将可以正常工作-但是如果objectID / Object仅在CSV文件之一?

因此,您不能使用zip(csvReader1, csvReader2),您  需要使用Date_Record作为键/索引ObjectID进行随机访问
当您提到大量数据时,我建议您使用SQL
如果要使用Python对象来执行此操作,请更改以下内容:

def __init__(self):
    self._data_store = {}

@data_store.setter
def data_store(self, data):
    ...
    self._data_store[record['ObjectID'] = record

  

问题:一个主题是使用csv文件和sql查询中的数据为每个唯一itemID创建BuildObject


  

检查您的代码,出现以下错误:

    class BuildObject(ObjectID):
NameError: name 'ObjectID' is not defined

您为什么要从ObjectID继承?
这些class在哪里定义?


请考虑以下内容:

class Data_Record():
    """
      This class object hold all data for ONE Record
    """
    def __init__(self, ObjectID, ObjectName):
        self.ObjectID= ObjectID
        self.ObjectName= ObjectName
        # ... (omitted for brevity)

class Data_Store():
    """
      This class object handels Data_Record, reading from csv or sql or anywhere
    """

    def __init__(self):
        # List to hold all Data_Record objects
        self._data_store = []

    # Access read only the Data_Record objects
    @property
    def data_store(self):
        return self._data_store

    # Add ONE Data_Record from either csv or sql or anywhere
    @data_store.setter
    def data_store(self, data):
        # Condition type(data)
        if isinstance(data, dict):
            record = Data_Record(**data)
        elif isinstance(data, list):
            record = Data_Record(**tuple(data))
        else:
            raise(ValueError, "Data of type({}) are not supported!".format(type(data)))

        self._data_store.append(record)

    # Method to read from csv
    def read_csv(self, fname1, fname2):
        # ... (omitted for brevity)
        csvReader1, csvReader2 = ([], [])
        for csv1, csv2 in zip(csvReader1, csvReader2):
            self.data_store = (csv2["ObjectName"], csv1["ObjectId"])

    # Method to read from sql
    def read_sql(self, sql, query):
        result = sql.query(query)
        for record in result:
            self.data_store = record

  

替代:不使用@property/getter/setter

这里read(...函数必须知道如何向Date_Record添加新的self.data_store对象。注意:self.data_store现在是 public 属性。
如果以后您决定不存储 ,则必须重写两个read(...函数。

class Data_Record():
    def __init__(self, data=None):
        # Condition type(data)
        if isinstance(data, dict):
            self.ObjectID = data['ObjectID']
            self.ObjectName = data['ObjectName']

        elif isinstance(data, list):
            # List have to be in predefined order
            # e.g ObjectID == Index 0 ObjectName == Index 1 etc.
            self.ObjectID = data[0]
            self.ObjectName = data[1]
        else:
            self.ObjectID = None
            self.ObjectName = None

class Data_Store():
    def __init__(self):
        self.data_store = []

def read_csv(self, fname1, fname2):
    for csv1, csv2 in zip(csvReader1, csvReader2):
        self.data_store.append(Data_Record((csv2["ObjectName"], csv1["ObjectId"])))

def read_sql(self, query):
    for record in SQL.query(query):
        self.data_store.append(Data_Record(record))