Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13436

Re: Storing strings and numbers properly in CSV files

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Storing strings and numbers properly in CSV files
Date 2014-04-23 01:27 +0200
Organization PointedEars Software (PES)
Message-ID <2105783.G0MQXoyxcR@PointedEars.de> (permalink)
References <92e0366c-bb9b-48cc-b239-d90f14865c8a@googlegroups.com> <5356d141$0$6707$9b4e6d93@newsspool2.arcor-online.net>

Show all headers | View raw


Christoph Michael Becker wrote:

> sonnichjensen@gmail.com wrote:
>> I am saving CSV files from a PHP app, but I face 2 problems:
>> 1. stock numbers are sometimes just numbers, but I'd like to keep them as
>> strings 2. prices are e.g. 5.2 which Excel translates as a date.
>> 
>> Say:
>> 
>> Item;Name;Price;Amount;Total
>> 123;Test;5.2;1;5.2
>> 124;Test2;1.2;2;2.4
>> Total;;;;=sum(e2:e3)
>> 
>> Just copy this into notepad and save with csv extension and you will see.
>> 
>> I'd like to have 123 and 124 as strings, and eg 5.2 as a float.
>> Can I format it better than this?
> 
> Most likely not.  CSV files store all values as strings, and it's up to
> the application that uses these data how to interpret them.  I would
> expect Excel to present a dialog where this can be configured, when you
> import a CSV file (IIRC that was so with Excel 2000; haven't used it
> since then).

It depends on how you import the data.  IIRC (it has been a year), opening 
the CSV directly with MS Excel (from Explorer or the browser) gives you no 
options, not even the choice of character encoding.  Therefore, it is 
imperative to quote all values where the type should not determined by 
Excel.  This is different if you use the Import Data dialog.

You would need to do this manually as fputcsv() can only either quote 
nothing or everything.  Or perhaps/probably ZF has a capability that could 
be reused; I have not checked.

> FWIW: regarding the CSV format there is the *informational* RFC 4180[1].

Fascinating.

> If you're looking particularly for interchange with Excel, you may
> consider to create .xls files instead of CSV files.

That would be .xlsx by now, and it is really not necessary for plain data.  
However, I would be surprised if CSVs could contain formulae for import.


PointedEars
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Storing strings and numbers properly in CSV files sonnichjensen@gmail.com - 2014-04-22 11:43 -0700
  Re: Storing strings and numbers properly in CSV files Jerry Stuckle <jstucklex@attglobal.net> - 2014-04-22 15:07 -0400
  Re: Storing strings and numbers properly in CSV files Michael Vilain <vilain@NOspamcop.net> - 2014-04-22 12:53 -0700
  Re: Storing strings and numbers properly in CSV files Christoph Michael Becker <cmbecker69@arcor.de> - 2014-04-22 22:29 +0200
    Re: Storing strings and numbers properly in CSV files Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-04-23 01:27 +0200
  Re: Storing strings and numbers properly in CSV files Richard Yates <richard@yatesguitar.com> - 2014-04-22 16:24 -0700
  Re: Storing strings and numbers properly in CSV files Jim Higgins <ILikeMy@Privacy.invalid> - 2014-04-24 19:21 +0000
  Re: Storing strings and numbers properly in CSV files richard <noreply@example.com> - 2014-05-02 20:48 -0400

csiph-web