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 1gYwfu-0007aP-L5 for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 17:27:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYwft-00049N-A0 for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 17:27:25 +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 1gYwfs-00049F-AH for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 17:27:25 +0000 Received: from nh601-vm13.bullet.mail.ssk.yahoo.co.jp ([182.22.90.22]) by magus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gYwfj-0002tq-Ea for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 17:27:20 +0000 Received: from [182.22.66.103] by nh601.bullet.mail.ssk.yahoo.co.jp with NNFMP; 17 Dec 2018 17:27:09 -0000 Received: from [182.22.91.128] by t601.bullet.mail.ssk.yahoo.co.jp with NNFMP; 17 Dec 2018 17:27:09 -0000 Received: from [127.0.0.1] by omp601.mail.ssk.yahoo.co.jp with NNFMP; 17 Dec 2018 17:27:09 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 280356.86842.bm@omp601.mail.ssk.yahoo.co.jp X-YMail-OSG: yLh57u8VM1mCl57G3I8W.C9A.vF_WmWA2A_3.WDmMafvL7WwXTll0hcdsHtO.Tk 8l5T.sV_BlrC.8uRln7q_IwRnknfEdyeFf1jAEDSnZOzgEznVUvYwwUruG.Qor0FPYj3ySoFh26Z xmiW_hY3MriBzlLeh8G46yURj4i4ikz_LNaBEZj3pquIx7VtWiEmSN4vSzW4l4yJlWmMKabyDfCw Pi.uUpWd5CY_.ZeCvX5DxgTiKDQhgAoXfPufCXGvOGAkyAPR3fTeiYXBDfPPlzhVOr3M9ZC3OuRi dsbc9Hn6.yA2YjLDS.6kuLnZEl3Nj8phz4p4gLY8W3qIp3jWWdyo6XR4DkHgoqmANdyJoGSMFzys uRxA4eVEjgSIDs3My7Gj24KcB3.JRNB8v.51N0_vCszTMWq821IIfuCSp18LSK85PMnOLMj2Nzoc 1XjJLtP2XSzbDWXI_71FQFP4FOozAoyRE4Qd5s2T_hB0TJatww0O2DqvvNxaKPMNKREMSOGd7pMn 3RrDkLJT4u2PO.XMa64Yh.aI4lSojEvtYhdvyxKk5hn6emBRAkuXKxwygacHJ1Ey58xwWLaxrPqy kXJmgkqbyk_r0OwBwBfv_6L3C8meOi8Y.4g.6i1.ZdRkkQg-- Received: from jws700101.mail.ssk.yahoo.co.jp by sendmailws617.mail.ssk.yahoo.co.jp; Tue, 18 Dec 2018 02:27:08 +0000; 1545067628.811 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1545067629; 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=kXs1bjrX3iAtyWcMmhRdTZLnwAduB+A96KS8v1sjts8=; b=qk/dlhRs1Coewx1N6sgV7bc7IX7Ym6rNo8MpAVr8udCW4E4MIY7K9KIX72iETljr P0rKlz5wDb/l4u642FKnPy6k4kd11M1Jfl1NDzJkdbJ7hebo+6CwRSlTan4sb1zOIVr 72aSmwZ7YJU/WpzlBnF5huEXAuAJ61qqnwM6jf9M= 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=bp76qywxbfMNBD657hHOXpntZe+ULPgjLwqXjRL7vC/mNE9BGYvWCrRss8ImnwgF 6uFdEmXjiJ7R8LY1qGwsSwPax1lUDBrZKlas+75cB9FI0t/r8jqbHIqZji3F/gXpFez g3tiF+2pGi+WSbrDACR/4l5CVtPnCKGQVA2jAK1E=; Date: Tue, 18 Dec 2018 02:27:08 +0900 (JST) From: Reply-To: To: Daniel Verite Cc: "pgsql-general@lists.postgresql.org" Message-ID: <485865655.585358.1545067628456.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: 2578 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Daniel, Thank you for the comments. I changed my line 7 to this: $line =3D implode("\t",$line). "\n"; Now, I get the following. Warning: pg_copy_from(): Copy command failed: ERROR: extra data after last = expected column CONTEXT: COPY test_table, line 1: "1=C2=A0=C2=A0=C2=A0 Denn= y's=C2=A0=C2=A0=C2=A0 orange juice=C2=A0=C2=A0=C2=A0 "1=C2=A0=C2=A0=C2=A0 5= 00 yen"=C2=A0=C2=A0=C2=A0 """Dear John""" "32"" TV"" in ----- 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