将数据从大量类写入文本文件的最有效方法是什么?

时间:2019-03-13 12:47:57

标签: python-3.x

我有大量的类实例列表,其中包含需要写入文本/ csv文件的数据。由于数量众多(超过1万个实例),我相信我应该避免在迭代循环中写入文件。下面是一个简化的示例,其中包括车辆类别定义和实例数组/列表。

类定义

class Vehicle:
    """docstring for Vehicle."""

    def __init__(self, colour, year, make, model, miles):

        self.colour = colour
        self.year = year
        self.make = make
        self.model = model
        self.miles = miles

    def __str__(self):
        return self.year + " " + self.make + " " self.model

    def get_year(self):
        return self.year

    def get_colour(self):
        return self.colour

仅为该示例创建大型车辆/列表

import random
colours = ['red', 'blue', 'green']
years = [2015, 2017, 2018]
makes = ['tesla', 'ford', 'toyota']

vl = []
for i in range(10000):
    colour = random.choice(colours)
    year = random.choice(years)
    make = random.choice(makes)
    miles = random.randint(1, 50000)
    car = Vehicle(colour, year, make, miles)
    vl.append(car)

# for vehicle in vl:
#    print(vehicle)

输出文件

以如下所示的csv格式从大量的Vehicle实例中写入数据的最有效方法是什么?

╔══════╦════════╦════════╦═══════╗
║ Year ║ Colour ║  Make  ║ Miles ║
╠══════╬════════╬════════╬═══════╣
║ 2015 ║ green  ║ tesla  ║ 1411  ║
║ 2018 ║ blue   ║ tesla  ║ 23314 ║
║ 2015 ║ blue   ║ ford   ║ 2035  ║
║ 2018 ║ blue   ║ toyota ║ 28915 ║
║ 2018 ║ green  ║ toyota ║ 22432 ║
║ ...  ║ ...    ║ ...    ║ ...   ║
╚══════╩════════╩════════╩═══════╝

1 个答案:

答案 0 :(得分:0)

这个答案是不可调节的,但是可以解决这种特殊情况。

import csv

class Vehicle:
    """docstring for Vehicle."""

    def __init__(self, color, year, make, miles): #removed model to simplify
        self.color = color
        self.year = year
        self.make = make
        self.miles = miles

    def get_miles(self):
        return self.miles

    def get_year(self):
        return self.year

    def get_color(self):
        return self.color

    def get_make(self):
        return self.make

colors = ['red', 'blue', 'green']
years = [2015, 2017, 2018]
makes = ['tesla', 'ford', 'toyota']

vl = []
for i in range(10000):
    color = random.choice(colors)
    year = random.choice(years)
    make = random.choice(makes)
    miles = random.randint(1, 50000)
    car = Vehicle(color, year, make, miles)
    vl.append(car.__dict__) #return your new object as a dictionnary and append it to your list

with open('cars.csv', 'w+') as output: #open or create a file named "cars.csv" and write in it
    fieldnames = ['year', 'color', 'make', 'miles'] #Same fields as your class "Vehicle"
    writer = csv.DictWriter(output, fieldnames=fieldnames) #object for csvWriter
    writer.writeheader()
    for car in vl: #looping in your list of objects
        writer.writerow(car) #writing a new row for each object in your list 'vl'

要很好地显示它,我让您可以在互联网上找到它;)