I have been working on using web2py to import csv files and find that
in general it works very well. You will need to be careful with
pulling in the colorant and amount fields - if you use the
csv.DictReader() to refer to columns in the CSV file by name instead
of index you'll find that you only get the value for the *last*
colorant and amount. In other words, just using row['Colorant'] and row
['Amount'] will very likely cause you to loose data - you've got 5
'Colorant' entries in a single row, so each one in a row simply over-
writes the earlier ones.  You will most likely need to use the
indexes, or just rename the column headers in the csv file so they are
unique.

~Brian

On Jan 8, 4:51 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Something like this?
>
> import csv
> reader = csv.reader(csvfile)
> header = None
> paints = {}
> colorants = {}
> for line in reader:
>     if not header: header = line
>     else:
>         row = dict([header[i],item) for i,item in enumerate(line)])
>         name = row['Color Name']
>         colorant = row['Colorant']
>         if not name in paints:
>             paints[name]=db.paint.insert(name=name)
>         if not colorant in colorants:
>             colorants[colorant]=db.colorant.insert(name=colorant)
>         db.paint_colorants.insert(paint_id=paints
> [name],colorant_id=colorants[colorant])
>
> On Jan 8, 4:10 am, Jason Brower <encomp...@gmail.com> wrote:
>
>
>
> > I have data like this in a csv file....
> > "Color Name", Base, Colorant, Amount, Colorant, Amount, Colorant,
> > Amount, Colorant, Amount, Colorant, Amount
> > "10 PORDRR",G,fo1,76,da1,32,ro1,111,yi1,1,,
> > It is for a paint database.
> > Is it at all possible to import that into this model?
> > Normally I would just take a few minutes and do it by hand.  But I have
> > 14,000+ entries, so that's out of the question.  Any ideas?  In
> > particular, how do you handle the variance in how many kinds of
> > colorants are added. Notice in this example that is only 4 colorants,
> > and 5+ could be in the page. (Currently from this extraction, only 5
> > colorants and their amounts.
>
> > db = SQLDB('sqlite://paint.sqlite')
>
> > db.define_table('paint',
> >         Field('name', length=30, requires=IS_NOT_EMPTY(), unique=True),
> >         Field('base_coat', length=3, requires=IS_NOT_EMPTY()),
> >         Field('hex_color', length=6))
>
> > db.define_table('colorant',
> >         Field('name', length=3, requires=IS_NOT_EMPTY()))
>
> > db.define_table('paint_colorants',
> >         Field('paint_id', db.paint),
> >         Field('colorant_id', db.colorant),
> >         Field('units', 'integer'))
>
> > db.paint_colorants.paint_id.requires=IS_IN_DB(db, 'paint.id',
> > '%(name)s')
> > db.paint_colorants.colorant_id.requires=IS_IN_DB(db, 'paint.id',
> > '%(name)s')
>
> > Best Regards,
> > Jason Brower
-- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en.


Reply via email to