> Have any of you guys imported excel/cvs by using
> Python/Django? I need to import 1000 products for a shopping
> site and I'd like to learn the fastest and easiest way to do
> so.
I do this day in and day out :)
I prefer tab-delimited because it's easy and clean, though
Python's built-in "csv" module can ease the pain of working with
csv (which I presume you mean, rather than CVS, which is the
Concurrent Versioning System)
I usually just write INSERT statements by hand, directly using
the DB interface for my backend (PostgreSQL in this case) and
then run a little Python code to iterate over the file and spew
the data into the system:
filename = 'foo.tab'
for i, line in enumerate(open(filename)):
if i == 0: continue # skip the header row
(fieldname1, fieldname2, fieldname3 ...
) = line.rstrip('\n').split('\t')
cursor.execute("""
INSERT INTO app_foo
(f1, f3, f2)
VALUES
(%s, %s, %s)
""", (fieldname1, fieldname3, fieldname2)
)
You may have to do lookups into other tables, etc, but this is
generally how I do it. Things can also get fancier if your input
file can have the columns in any arbitrary order. The above simply
From within a running Django app, you can make use of the ORM to
ease some of the annoyances, but when bulk-loading the data
repeatedly (several thousand rows of data, multiple times per
day), I personally like the control of raw SQL to eke out the
last bit of performance from my DB connection.
I've seen some tools that try to ease this for you, but I've
never been impressed by them, as I end up spending as much time
fiddling with them as it would take me to bang out some python
code to do the same thing.
-tim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---