FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Storing strings and numbers properly in CSV files
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Storing strings and numbers properly in CSV files [message #185625 is a reply to message #185620] Tue, 22 April 2014 20:29 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
sonnichjensen(at)gmail(dot)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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Mock HTTP servers for unit tests.
Next Topic: scandir problem
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 04:40:18 GMT 2024

Total time taken to generate the page: 0.04689 seconds