Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #13427 > unrolled thread
| Started by | sonnichjensen@gmail.com |
|---|---|
| First post | 2014-04-22 11:43 -0700 |
| Last post | 2014-05-02 20:48 -0400 |
| Articles | 8 — 8 participants |
Back to article view | Back to comp.lang.php
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
| From | sonnichjensen@gmail.com |
|---|---|
| Date | 2014-04-22 11:43 -0700 |
| Subject | Storing strings and numbers properly in CSV files |
| Message-ID | <92e0366c-bb9b-48cc-b239-d90f14865c8a@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2014-04-22 15:07 -0400 |
| Message-ID | <lj6ekp$7i1$1@dont-email.me> |
| In reply to | #13427 |
On 4/22/2014 2:43 PM, sonnichjensen@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.
>
> 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
>
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
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Michael Vilain <vilain@NOspamcop.net> |
|---|---|
| Date | 2014-04-22 12:53 -0700 |
| Message-ID | <vilain-ECC3A7.12530622042014@news.individual.net> |
| In reply to | #13427 |
In article <92e0366c-bb9b-48cc-b239-d90f14865c8a@googlegroups.com>, sonnichjensen@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. > > 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 The quoting rules with CSV must be somewhere on the web. Try googling it. It should explain when you have to put entries in quotes. If all else fails, just quote everything. -- DeeDee, don't press that button! DeeDee! NO! Dee... [I filter all Goggle Groups posts, so any reply may be automatically ignored]
[toc] | [prev] | [next] | [standalone]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-04-22 22:29 +0200 |
| Message-ID | <5356d141$0$6707$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #13427 |
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). 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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-04-23 01:27 +0200 |
| Message-ID | <2105783.G0MQXoyxcR@PointedEars.de> |
| In reply to | #13431 |
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>
[toc] | [prev] | [next] | [standalone]
| From | Richard Yates <richard@yatesguitar.com> |
|---|---|
| Date | 2014-04-22 16:24 -0700 |
| Message-ID | <b9udl998orot06slumc7vvgb2gehuf056o@4ax.com> |
| In reply to | #13427 |
On Tue, 22 Apr 2014 11:43:02 -0700 (PDT), sonnichjensen@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. > >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
[toc] | [prev] | [next] | [standalone]
| From | Jim Higgins <ILikeMy@Privacy.invalid> |
|---|---|
| Date | 2014-04-24 19:21 +0000 |
| Message-ID | <fcnil9lcl469g55rk6oacin1t7urev2jhm@4ax.com> |
| In reply to | #13427 |
On Tue, 22 Apr 2014 11:43:02 -0700 (PDT), in <92e0366c-bb9b-48cc-b239-d90f14865c8a@googlegroups.com>, 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 Modify the PHP app to place a "=" in front of each number you want to keep as a string. Simple example is ="123" Expanded example below. >2. prices are e.g. 5.2 which Excel translates as a date. They don't appear as a date when tested here. Keep reading. >Say: > >Item;Name;Price;Amount;Total >123;Test;5.2;1;5.2 >124;Test2;1.2;2;2.4 >Total;;;;=sum(e2:e3) The above is not proper CSV format. Modify your PHP program to produce output that has each value within double quotes and each of these quoted values separated by commas... like the following. "Item","Name","Price","Amount","Total" "123","Test","5.2","1","5.2" "124","Test2","1.2","2","2.4" "Total","","","","=sum(e2:e3)" Read CSV spec if you have to deal with commas and/or quotes in your actual data. And don't expect Excel to be fully cooperative. If something doesn't work, you may need to Google for solutions specific to Excel. >Just copy this into notepad and save with csv extension and you will see. Your unaltered sample file (with preceding > removed) resulted in a 4-line spreadsheet with all data for that line in column A of the resulting Excel... as string data. My revision (above) to your output results in items in separate columns, but "Item" data that you want as a string is numeric. The following is the output you want from your PHP program in order for the "Item" data to appear as strings when imported into Excel. You just stick an "=" in front of the quoted value to make Excel see it as a string. If you wanted the price as a string then an example of what should be between the commas would be ="5.2" ="Item","Name","Price","Amount","Total" ="123","Test","5.2","1","5.2" ="124","Test2","1.2","2","2.4" "Total","","","","=sum(e2:e3)" >I'd like to have 123 and 124 as strings, and eg 5.2 as a float. My little rev (immediately above) to your output displays 5.2 as a float, not a string. >Can I format it better than this? Try my examples and I think all your problems will go away.
[toc] | [prev] | [next] | [standalone]
| From | richard <noreply@example.com> |
|---|---|
| Date | 2014-05-02 20:48 -0400 |
| Message-ID | <skb3q1h4sgkw.fo590i1apqv6$.dlg@40tude.net> |
| In reply to | #13427 |
On Tue, 22 Apr 2014 11:43:02 -0700 (PDT), sonnichjensen@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. > > 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 "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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web