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 1gZ7jc-0001ru-Vz for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 05:16:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gZ7jb-0001be-KC for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 05:15:59 +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 1gZ7ja-0001aR-Jm for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 05:15:59 +0000 Received: from nh502-vm10.bullet.mail.kks.yahoo.co.jp ([183.79.56.155]) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gZ7jN-0003d7-Or for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 05:15:54 +0000 Received: from [183.79.100.138] by nh502.bullet.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:15:39 -0000 Received: from [183.79.101.120] by t501.bullet.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:15:39 -0000 Received: from [127.0.0.1] by omp507.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:15:39 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 279255.33626.bm@omp507.mail.kks.yahoo.co.jp X-YMail-OSG: .GYbagMVM1lW1.95xm9kQUyuUIYyxZJtxfl.Uf.2yoPAwUDL7NYS9zl2VOEjD3X psf.F.rcHkpTcekfQfLrXrFPSOG1S8nt_VTD9S_Fsgm2E.vELaGyINrQQU1Lt6bBJ6MrR_dVsR97 faV46cyHKyG2Iz418NTCt1JJf60_ApG2b0beDfkVzkoRqVM.n5GFqmMVzHwFp4JYnFvh2GR.wqOo QEkrmwIkMISxsk7MYOJlI8pLGhobHb_AwzQTWoktQAmmBgNLyhgipZocEha8unr67PG2PHcSoYAG Vs3b9J0I6rNEpNWySuK_2N13Ag1r30t7XQykEiI61F32kdttBeXLH0y2a82kK2hcTvFWXsYttb24 JavVkAAI2FoLGkeh.z5S3y.5vMqK0IAB0tsi5FPQSYLvpC3i.OHEafrUKmlQOzHvD3VoK_OecTMx ROepSUjkFlXpxyzlUu0kBAxSacs3FAz5KyRAm3orTxBh9JVjRIDbzksCY5IDRARQD2v3czl81F0S GAQ20Je6h8cbd8pNWtf_ORruyvFAjKujm3ok8HkTwM4DfSKpsbQFJWKru87d112gAvHQzcMFWAdr wYE7.g0blYVkw7LcvT.D32G05SAWBHK6RyEh32hm_NTSjGA-- Received: from jws700004.mail.kks.yahoo.co.jp by sendmailws505.mail.kks.yahoo.co.jp; Tue, 18 Dec 2018 14:15:38 +0000; 1545110138.795 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1545110139; 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=NdGDZKatpiEModQGOeN+jXHUMbQ5u2KJsM2ruFpLFC8=; b=ZOaSdjFvE3GAjSF+aUQpUwpoChTGBEGJfnhVJBQI5Svblf2ln9RZpwDrFRgh+dKO dOXPnCNNh0NA5FnZfVt4FNi5v9O/DyKwjIRZaqS74uzrQ9O3qSUvMDTGxRCPoYDryYf JiZzW33a0vxqatNw2h31EM8qrLJ/ey+eJSV5q5Ss= 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=Vxgo7/JeS2htk4Al3ZC54VZRBZ3h1L8YZJ3E3GUcxOFB2f/p5E1TxatC0CvSL9vb oB+y4dBF1ZovhAftmWkJLHzqeBDKtO5DjDGVaWLy6twuC7NGj+kSRLceHy3b0lBbHYu vcTAWBR3S3hA6GRY8HkwErKwWUDhtsgMuhq4uvRE=; Date: Tue, 18 Dec 2018 14:15:38 +0900 (JST) From: Reply-To: To: Daniel Verite Cc: "pgsql-general@lists.postgresql.org" Message-ID: <369015105.594386.1545110138095.JavaMail.yahoo@mail.yahoo.co.jp> In-Reply-To: <8ed63cae-4d67-4b3d-9e33-3a212611191f@manitou-mail.org> References: <904538479.543179.1545050043907.JavaMail.yahoo@mail.yahoo.co.jp> <8ed63cae-4d67-4b3d-9e33-3a212611191f@manitou-mail.org> 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: 2824 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello Daniel! This afternoon, I tried it again, and yes, your suggestion that I use a tab= =20 delimited symbol ("\t") instead of a space for the implode works flawlessly= . Yesterday, I reported I got "Warning: pg_copy_from(): Copy command failed: = 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) { Today, I used this: =C2=A0=C2=A0=C2=A0=C2=A0 while (($line =3D fgetcsv($fileRead, 0, "\t")) != =3D=3D FALSE) { .. and now I can see my data saved in the database without those extra doub= le quotes. It made my day! Thank you. ----- 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 te= xt file that contains comma and double quotes >=20 > =C2=A0=C2=A0=C2=A0 =C2=A0 wrote: >=20 >> When I save that Excel as a tab delimited text file, I get this:rec_no= =C2=A0=20 >> item1=C2=A0 =C2=A0 item2=C2=A0 =C2=A0 item3=C2=A0 =C2=A0 item4=C2=A0 = =C2=A0 item5 >> 1=C2=A0 =C2=A0 Denny's=C2=A0 =C2=A0 orange juice=C2=A0 =C2=A0 "1,500 ye= n"=C2=A0 =C2=A0=20 > """Dear John"""=C2=A0 =C2=A0 "32"" >> 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 >> 5=EF=BC=8Ewhile (($line =3D fgetcsv($fileRead, 0, "\t")) !=3D=3D FALSE)= {=20 > //0 means I can >> read row whatever its length >> 6=EF=BC=8E=C2=A0 =C2=A0 if($row =3D=3D 1){ $row++; continue; } //skip h= eader >> 7=EF=BC=8E=C2=A0 =C2=A0 $line =3D implode(" ",$line). "\n"; >> 8=EF=BC=8E=C2=A0 =C2=A0 $twoDarray[] =3D $line; >> ... >> 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