tags:

views:

37

answers:

2

I would like to generate a TSV file from a list of vehicles....I'll also be making some conversions on the data e.g. if value for transmission is "Automatic" then this value will be rendered as "A" or if mileage is in miles, then multiply this value by 1.6.

What's the best way to go about this?

UPDATE

OK...so I've written the script as below:

from django.conf import settings
import myapp.settings
setup_environ(myapp.settings)

from django.db import models
from myapp.vehicles.models import Vehicle

import csv

data = Vehicle.objects.all().exclude(status__status='Internal Use').
   exclude(status__status='Sold').order_by('-common_vehicle__year',
   'common_vehicle__series__model__manufacturer__manufacturer',
   'common_vehicle__series__model__manufacturer__manufacturer_popularity')


vehicles = csv.writer(open('cars.csv', 'w'), delimiter='\t')
vehicles.writerow(["VIN","Stock","Year","Make","Model",
    "TrimPackage","Transmission","DriveType","Odometer","OdometerType",
    "Doors","BodyStyle","EngineType","Exterior","Interior","Condition",
    "Certified","FuelType","Price"])
for vehicle in data:
  vehicles.writerow(["1130","26919",vehicle.vin,vehicle.stock_number,
  vehicle.common_vehicle.year.year,
  vehicle.common_vehicle.series.model.manufacturer.manufacturer,
  vehicle.common_vehicle.series.model.model,vehicle.common_vehicle.series.series,
  transmission,vehicle.common_vehicle.drive_train.drive_train,
  vehicle.odometer_reading,"KM",vehicle.common_vehicle.body_style.doors,
  body_style,engine_type,vehicle.exterior_colour.exterior_colour,
  vehicle.interior_colour.interior_colour,"E","Y",fuel_type,"0"])

However, when I try to run the script I get this error: ImportError: No module named myapp.settings

UPDATE 2 Just seen my mistake...all is well now

+3  A: 

Use the CSV support in Python.

Change the dialect delimiter to tab.

The linked page has plenty of examples.

celopes
This looks more like it....let me try it out and see if it works out. Thanks
Stephen
A: 

Do you need a static file? or are you looking to dynamically generate this information based on something else in you Django app?

Either way I think the Django template system would work just fine for this. All you need to do is design a template with the format you want the data in. If you need a static file, make a small script that saves the result of the template to a static file.

Steven Potter