pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: s400t@yahoo.co.jp
To: Daniel Verite <daniel@manitou-mail.org>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted text file  that contains comma and double quotes
Date: Tue, 18 Dec 2018 14:18:49 +0900 (JST)
Message-ID: <115096948.607924.1545110330008.JavaMail.yahoo@mail.yahoo.co.jp> (raw)
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>

>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" <s400t@yahoo.co.jp>
> To: Daniel Verite <daniel@manitou-mail.org>
> Cc: "pgsql-general@lists.postgresql.org" <pgsql-general@lists.postgresql.org>
> Date: 2018/12/18, Tue 14:15
> Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted text file  that contains comma and double quotes
> 
> Hello Daniel!
> 
> This afternoon, I tried it again, and yes, your suggestion that I use a tab 
> 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:
>      while (($line = fgetcsv($fileRead, "\t")) !== FALSE) {
> 
> 
> Today, I used this:
>      while (($line = fgetcsv($fileRead, 0, "\t")) !== FALSE) {
> 
> 
> .. and now I can see my data saved in the database without those extra double 
> quotes.
> 
> It made my day!
> 
> Thank you.
> 
> 
> 
> 
> 
> ----- Original Message -----
>>  From: Daniel Verite <daniel@manitou-mail.org>
>>  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 
> file  that contains comma and double quotes
>> 
>>        <s400t@yahoo.co.jp> wrote:
>> 
>>>   When I save that Excel as a tab delimited text file, I get 
> this:rec_no  
>>>   item1    item2    item3    item4    item5
>>>   1    Denny's    orange juice    "1,500 yen"    
>>  """Dear John"""    "32""
>>>   TV"(As seen when I opened that file with Notepad)
>> 
>>  This looks good. Fields are properly enclosed and double quotes
>>  in contents are doubled, as expected in CSV.
>> 
>>>   5.while (($line = fgetcsv($fileRead, 0, "\t")) !== 
> FALSE) { 
>>  //0 means I can
>>>   read row whatever its length
>>>   6.    if($row == 1){ $row++; continue; } //skip header
>>>   7.    $line = implode(" ",$line). "\n";
>>>   8.    $twoDarray[] = $line;
>>>   ...
>>>   14.if (pg_copy_from($con, $tableName, $twoDarray) !== FALSE) {
>> 
>>  It goes wrong at line 7. pg_copy_from() expects lines in the 
>>  COPY "text format" documented at
>>  https://www.postgresql.org/docs/current/sql-copy.html 
>> 
>>  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.
>> 
>>  These replacements can all be done by a single strtr() call in php.
>> 
>> 
>>  Best regards,
>>  -- 
>>  Daniel Vérité
>>  PostgreSQL-powered mailer: http://www.manitou-mail.org 
>>  Twitter: @DanielVerite
>> 
> 





view thread (7+ messages)  latest in thread

Message-ID: <115096948.607924.1545110330008.JavaMail.yahoo@mail.yahoo.co.jp>
Permalink:  ../115096948.607924.1545110330008.JavaMail.yahoo@mail.yahoo.co.jp/
Also on:    postgresql.org/message-id/115096948.607924.1545110330008.JavaMail.yahoo@mail.yahoo.co.jp

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: s400t@yahoo.co.jp, daniel@manitou-mail.org
  Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted text file  that contains comma and double quotes
  In-Reply-To: <115096948.607924.1545110330008.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> <115096948.607924.1545110330008.JavaMail.yahoo@mail.yahoo.co.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox