Re: CSV import

From: Oliver Vecernik <vecernik(at)aon(dot)at>
To: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: CSV import
Date: 2003-01-29 05:50:42
Message-ID: 3E376BB2.4010302@aon.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Oliver Vecernik schrieb:

> Hi again!
>
> After investigating a little bit further my CSV import couldn't work
> because of following reasons:
>
> 1. CSV files are delimited with CR/LF
> 2. text fields are surrounded by double quotes
>
> Is there a direct way to import such files into PostgreSQL?

The answer seems to be no. But after googeling a bit a found a wonderful
Python module called csv at:

http://www.object-craft.com.au/projects/csv/

A minimal script called 'csv2tab.py' for conversion to a tab delimited
file could be:

#!/usr/bin/env python

import csv
import sys

def convert(file):
try:
f = open(file, 'r')
lines = f.readlines()
p = csv.parser()
for line in lines:
print '\t'.join(p.parse(line))
except:
print 'Error opening file!'

if __name__ == '__main__':
convert(sys.argv[1]);

Regards,
Oliver

--
VECERNIK Datenerfassungssysteme
A-2560 Hernstein, Hofkogelgasse 17
Tel.: +43 2633 47530, Fax: DW 50
http://members.aon.at/vecernik

In response to

  • CSV import at 2003-01-28 14:46:07 from Oliver Vecernik

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2003-01-29 06:03:02 Re: Cross-table constraints
Previous Message Wei Weng 2003-01-29 04:39:47 Re: converting microsoft sql server 2000 sql-code for postgresql