Re: Assistance with importing a csv into a PostgreSQL database

From: Michael Wood <esiotrot(at)gmail(dot)com>
To: Intengu Technologies <sindile(dot)bidla(at)gmail(dot)com>
Cc: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Assistance with importing a csv into a PostgreSQL database
Date: 2009-08-06 12:35:56
Message-ID: 5a8aa6680908060535m6c8ed0e5g9dd4441a60128e36@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

2009/8/6 Intengu Technologies <sindile(dot)bidla(at)gmail(dot)com>:
> Fellow South African indeed ;)
>
> I run windows and have installed Python - forgive me I am not a
> programmer and my data is on an external HD.
> I then modified your script to be like this

You will need double backslashes if you specify the full path.

e.g.:
infile = open("F:\\data\\myfile.csv")

This is because the backslash is a quoting character in Python (and
many other languages).

If this still doesn't work, you should be able to change back to just
filenames instead of the full path, put the script and myfile.csv in
the same directory and then double-click on the script.

> import csv
>>
>> def get_csv_writer(filename):
>>     return csv.writer(open(filename, "w"), quoting=csv.QUOTE_ALL)
>>
>> infile = open("F:\data\myfile.csv")
>> table = {"1": get_csv_writer("F:\data\myfiletable1.csv"),
>>          "2": get_csv_writer("F:\data\myfiletable2.csv"),
>>          "3": get_csv_writer("F:\data\myfiletable3.csv"),
>>          "4": get_csv_writer("F:\data\myfiletable4.csv"),
>>          "5": get_csv_writer("F:\data\myfiletable5.csv"),
>>          "6": get_csv_writer("F:\data\myfiletable6.csv")}
>>
>> reader = csv.reader(infile)
>> for line in reader:
>>     table[line[0]].writerow(line[1:])
>
> I then saved this as importcsv.py
>
> Opened it in python IDE, but I am getting syntax errors.

If the advice above does not help, send the syntax errors. This is of
course not a Python support list, but I hope nobody minds too much :)

--
Michael Wood <esiotrot(at)gmail(dot)com>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tim Greenwood 2009-08-06 18:26:10 Checking my understanding of locales
Previous Message Michael Wood 2009-08-06 12:27:52 Re: Assistance with importing a csv into a PostgreSQL database