Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZ7mY-00020T-Bp for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 05:19:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gZ7mX-0005oz-2b for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 05:19:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZ7mW-0005os-Qk for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 05:19:00 +0000 Received: from nh502-vm5.bullet.mail.kks.yahoo.co.jp ([183.79.56.150]) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gZ7mT-0003hq-GM for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 05:19:00 +0000 Received: from [183.79.100.138] by nh502.bullet.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:18:55 -0000 Received: from [183.79.100.135] by t501.bullet.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:18:55 -0000 Received: from [127.0.0.1] by omp504.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:18:55 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 81999.11760.bm@omp504.mail.kks.yahoo.co.jp X-YMail-OSG: 5myvhFUVM1kpwCHmoYz7CqpMqSJ5uxvNDXGyDNVy5wa7.JkNnFps4xsk9fCzIx5 7NN8cL4RIj4X1pWzYkNsbHXYY4ZfYVM.7LwC5ta8N0iuKDj0XzZzHz2p8UURqYm0smIVawWl57Fa g4WyRfggDAvvtUf_HZtueQ5ghMbw2c0ue3AjVgsbpo4zrZua_0AqqQYrEsV5TTr8KbgwLZ6N2Hea nWDrm8RnVWvOeI.LTNYaWjMR27a9vAmysY2BE37yfD8pqjDgpSWLRitiHkVD406jY4TYVsKVcIZn EhZm9eva8oJlNVH68bpdyhrRXOdSenDe_EVnf_Jj.nltRDoQuJNmx8udvZIlZeCPEbDSkcTDrOow EvBv4qNSKVa60UYAf1O.986ssPWZEgTK7xll8vd_Vu.Mre2QUFDhOuTgeOMUCKCTZeXWNwCH9YSO ZA5A.A3BBprMcgVmA9bT7m2aFQBerVNNOq0e457.jzGqaa6euYPlUVqkcT2jPv.FxAEi0SMGVagl jhziv8iyuxwIzLBMGWgRCtyX_KbvZfiLkWUKW9R5TYpxtsAgdyqTwJabWx2R6Ja1GARGd6zCO6fG TRDQ4bvMJXTlUo8CTUJrePLe7AK7FegEb7a7FI3FkP_AkEA-- Received: from jws700104.mail.ssk.yahoo.co.jp by sendmailws618.mail.ssk.yahoo.co.jp; Tue, 18 Dec 2018 14:18:50 +0000; 1545110330.439 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1545110334; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:To:Cc:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=gYuzrtDSFyvhnw/SLvjGvYvE/sKEshXNP1azi1A35Ps=; b=Q7jxTVwhyW81yb5Nwc64f6HKRPyFJ5qH1Vs+XHzzTCa5pgv1U+DnvzfldBDpWVgD JRcVV9xGxcdUX+d7/RjvkD+7snKOciCeKLc5Ca11MaVYxH0vOmO4unmIGkRz7a/GR8S GNNM6zHnvqfiRHbKDobcC05b5yqQv3as1YDebY/k= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:Cc:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=mhlI03IbjqdPJOfbuLLC7WMqbT2ithLq1SQz8uBXYrr2jNqX6Dx3BjOl/2oTjpKD XXgG7f20Dq4AsnyfXnHB5TWH9mx5D68aQCfhlDr6QFGhDXxFv731JpdAalAqOUqc2ZK 9NPNcyN3R5ZxJjt6R8wWbsTsNaGef+GoFnqswCWo=; Date: Tue, 18 Dec 2018 14:18:49 +0900 (JST) From: Reply-To: To: Daniel Verite Cc: "pgsql-general@lists.postgresql.org" Message-ID: <115096948.607924.1545110330008.JavaMail.yahoo@mail.yahoo.co.jp> In-Reply-To: <369015105.594386.1545110138095.JavaMail.yahoo@mail.yahoo.co.jp> References: <904538479.543179.1545050043907.JavaMail.yahoo@mail.yahoo.co.jp> <8ed63cae-4d67-4b3d-9e33-3a212611191f@manitou-mail.org> <369015105.594386.1545110138095.JavaMail.yahoo@mail.yahoo.co.jp> Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted text file that contains comma and double quotes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Length: 3572 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >and now I can see my data saved in the database without those extra double= quotes. .. and with my comma intact as well. ----- Original Message ----- > From: "s400t@yahoo.co.jp" > To: Daniel Verite > Cc: "pgsql-general@lists.postgresql.org" > Date: 2018/12/18, Tue 14:15 > Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted te= xt file that contains comma and double quotes >=20 > Hello Daniel! >=20 > This afternoon, I tried it again, and yes, your suggestion that I use a t= ab=20 > delimited symbol ("\t") instead of a space for the implode works=20 > flawlessly. >=20 > Yesterday, I reported I got "Warning: pg_copy_from(): Copy command failed= :=20 > ERROR: extra data after last expected..". > That was because while reading file, I had used this: > =C2=A0=C2=A0=C2=A0=C2=A0 while (($line =3D fgetcsv($fileRead, "\t")) !=3D= =3D FALSE) { >=20 >=20 > Today, I used this: > =C2=A0=C2=A0=C2=A0=C2=A0 while (($line =3D fgetcsv($fileRead, 0, "\t")) != =3D=3D FALSE) { >=20 >=20 > .. and now I can see my data saved in the database without those extra do= uble=20 > quotes. >=20 > It made my day! >=20 > Thank you. >=20 >=20 >=20 >=20 >=20 > ----- Original Message ----- >> From: Daniel Verite >> To: s400t@yahoo.co.jp >> Cc: pgsql-general@lists.postgresql.org >> Date: 2018/12/18, Tue 00:35 >> Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted = text=20 > file=C2=A0 that contains comma and double quotes >>=20 >> =C2=A0=C2=A0=C2=A0 =C2=A0 wrote: >>=20 >>> =C2=A0 When I save that Excel as a tab delimited text file, I get=20 > this:rec_no=C2=A0=20 >>> =C2=A0 item1=C2=A0 =C2=A0 item2=C2=A0 =C2=A0 item3=C2=A0 =C2=A0 item4= =C2=A0 =C2=A0 item5 >>> =C2=A0 1=C2=A0 =C2=A0 Denny's=C2=A0 =C2=A0 orange juice=C2=A0 =C2=A0 "1= ,500 yen"=C2=A0 =C2=A0=20 >> """Dear John"""=C2=A0 =C2=A0 "32"" >>> =C2=A0 TV"(As seen when I opened that file with Notepad) >>=20 >> This looks good. Fields are properly enclosed and double quotes >> in contents are doubled, as expected in CSV. >>=20 >>> =C2=A0 5=EF=BC=8Ewhile (($line =3D fgetcsv($fileRead, 0, "\t")) !=3D=3D= =20 > FALSE) {=20 >> //0 means I can >>> =C2=A0 read row whatever its length >>> =C2=A0 6=EF=BC=8E=C2=A0 =C2=A0 if($row =3D=3D 1){ $row++; continue; } /= /skip header >>> =C2=A0 7=EF=BC=8E=C2=A0 =C2=A0 $line =3D implode(" ",$line). "\n"; >>> =C2=A0 8=EF=BC=8E=C2=A0 =C2=A0 $twoDarray[] =3D $line; >>> =C2=A0 ... >>> =C2=A0 14=EF=BC=8Eif (pg_copy_from($con, $tableName, $twoDarray) !=3D= =3D FALSE) { >>=20 >> It goes wrong at line 7. pg_copy_from() expects lines in the=20 >> COPY "text format" documented at >> https://www.postgresql.org/docs/current/sql-copy.html=20 >>=20 >> It implies that: >> - since your call to pg_copy_from() doesn't specify a delimiter >> it uses tab, not a space, so implode() must be passed a tab, >> not a space. >> - if there are backslashes in the contents they must be quoted >> by doubling them. >> - if there are newline or carriage return characters in the contents >> they must be replaced by \n and \r respectively, so as to >> not be confused with an end of record. >> - if there are tabs in the contents they must be replaced by \t. >>=20 >> These replacements can all be done by a single strtr() call in php. >>=20 >>=20 >> Best regards, >> --=20 >> Daniel V=C3=A9rit=C3=A9 >> PostgreSQL-powered mailer: http://www.manitou-mail.org=20 >> Twitter: @DanielVerite >>=20 >=20