Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 8A4CB477162 for ; Wed, 29 Jan 2003 05:42:44 -0500 (EST) Received: from stan.ringways.co.uk (hub.ringways.co.uk [213.38.87.130]) by postgresql.org (Postfix) with ESMTP id E140A47724F for ; Wed, 29 Jan 2003 05:06:33 -0500 (EST) Received: from gary.ringways.co.uk ([10.1.1.2]) by stan.ringways.co.uk with esmtp (Exim 3.22 #1) id 18dpDH-0002uC-00; Wed, 29 Jan 2003 10:12:59 +0000 Content-Type: text/plain; charset="iso-8859-15" From: Gary Stainburn Organization: Ringways Garages Ltd To: Oliver Vecernik , pgsql-sql Subject: Re: CSV import Date: Wed, 29 Jan 2003 10:04:48 +0000 User-Agent: KMail/1.4.3 References: <3E3697AF.2030509@aon.at> <3E376BB2.4010302@aon.at> In-Reply-To: <3E376BB2.4010302@aon.at> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <200301291004.48919.gary.stainburn@ringways.co.uk> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/482 X-Sequence-Number: 11347 On Wednesday 29 January 2003 5:50 am, Oliver Vecernik wrote: > 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? Here's a simple command that will take "hello","world","splat","diddle" "he said "hello world" to ","his mate" and convert it to the following tab delimited file that can be COPYed using psql. It even handles quotes inside fields. (^m and ^i are done by typing CTRL+V CTRL+M and CTRL+V CTRL+I) hello world splat diddle he said "hello world" to his mate sed 's/^"//' t1.txt Gary > > 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 -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000