Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Storing strings and numbers properly in CSV files

7 views
Skip to first unread message

sonnic...@gmail.com

unread,
Apr 22, 2014, 2:43:02 PM4/22/14
to
Hi

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?

WBR
Sonnich

Jerry Stuckle

unread,
Apr 22, 2014, 3:07:07 PM4/22/14
to
Unfortunately, while there are some guidelines on how .CSV files should
be written, there are few "hard and fast" rules. About the only ones
are that if a field contains the field and/or row separator character
(by default comma and newline, respectively), that field must be
enclosed in double quotes. Also, if a field contains double quotes,
("), the quotes must be doubled ("").

Because there are so few rules, the program that imports the file is
pretty much free to do what it wants with the file. You might try
enclosing the numbers you want as strings in quotes (i.e. "123"); I
don't know if this will work or not (I don't use Excel).

Other than that, you could try an Excel newsgroup or forum to see if
they have any suggestions on how to format your file so that Excel will
interpret it as you wish.

Wish I could be of more help.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================
Message has been deleted

Christoph Michael Becker

unread,
Apr 22, 2014, 4:29:56 PM4/22/14
to
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). FWIW: regarding the CSV format there is the
*informational* RFC 4180[1].

If you're looking particularly for interchange with Excel, you may
consider to create .xls files instead of CSV files. There are libraries
helping with this task, e.g. PEAR's Spreadsheet_Excel_Writer[2].

[1] <http://tools.ietf.org/html/rfc4180>
[2]
<http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php>

--
Christoph M. Becker

Richard Yates

unread,
Apr 22, 2014, 7:24:14 PM4/22/14
to
On Tue, 22 Apr 2014 11:43:02 -0700 (PDT), sonnic...@gmail.com
wrote:

>Hi
>
>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.

Excel will handle this exactly as you want if, instead of opening it,
you go to the Data tab and 'Get External Data from Text.' It will let
you specify the column delimiters (in this case the semicolon) and
data types.

Once it is imported you can tell Excel to interpret columns any way
you want.

Thomas 'PointedEars' Lahn

unread,
Apr 22, 2014, 7:27:40 PM4/22/14
to
Christoph Michael Becker wrote:

> sonnic...@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$8300...@news.demon.co.uk>
Message has been deleted

richard

unread,
May 2, 2014, 8:48:18 PM5/2/14
to
On Tue, 22 Apr 2014 11:43:02 -0700 (PDT), sonnic...@gmail.com wrote:

"1","2","3"
Which is a string and which is a constant?
The CSV file does not care.

The application will determine which is which.
0 new messages