How do Import a csv file that contains strings

I have a file that contains numbers as US formmated $$$ such as $1,123,123,123 I have a perl script on one of our linux servers that creates the csv file as

"Total Dollars","$1,123,123,123","some other string","etc" but when I import the file into numbers

Unnamed Table

Average Price'

$160

686'

$222

482'

$114

820'

$188

062'


I cant find a setting anywhere that says to ignore commas inside a string. This works in most other applications.

MacBook Pro, Mac OS X (10.7.2)

Posted on Jan 25, 2012 12:33 PM

Reply
13 replies

Jan 25, 2012 12:41 PM in response to KOENIG Yvan

It does have a .csv extension Here is a sample line from the file

'Average Price','$160,686','$222,482','$114,820','$188,062'

I put single quotes around every item.


It appears to see the first single quote because it does remove it. But it is not removing the trailing single quote. and the single quotes dont seem to protect the numbers.


Message was edited by: bradleyfromjacksonville

Jan 25, 2012 12:47 PM in response to bradleyfromjacksonville

Values embedding commas must be enclosed by double quotes as what you wrote in your first message.

If your server put single quotes, it's the wrongdoer.


PS : the single quotes are used for an other task.

When There is one as the 1st char of a val, it tell the app that it must treat the rest as a string (not as a number, a date …)

This is why you loose it in cells. It's also why in the cells containing only numeric characters the content is aligned on the left which flags strings.


Yvan KOENIG (VALLAURIS, France) mercredi 25 janvier 2012

iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.2

My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

Jan 25, 2012 12:48 PM in response to bradleyfromjacksonville

BEfore improting into numbers do a search and replace. Replace ,'$ with ;'$

Then remove all the commas. Replace "," with ""

You should be now able to import the file using ; as separator or if you prefer you can do again the change of ; with ,

In this way you could even remove the $ and put it back later in numbers with the right formatting.

Jan 25, 2012 12:49 PM in response to KOENIG Yvan

the problem is the program I wrote is in perl and single/double quotes have significance for example

print REPORT3 "'".$county."',,,,\n";


Its kinda hard to read here but this is a double quote, single quote, double quote then period. Double quotes tell the perl interperter to not process whats in between them. Is there a way to tell numbers to process using single quotes instead of double quotes?

Jan 25, 2012 12:50 PM in response to WALTER-MILANO-ITALY

Hello Walter


You are describing the Numbers behavior when the decimal character is the comma (as it is in France or in Italy).

In this cas values must be separated by semi-colon.


But this doesn't apply when the decimal character is period as it is in the OP's machine.


Yvan KOENIG (VALLAURIS, France) mercredi 25 janvier 2012

iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.2

My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

Jan 25, 2012 12:55 PM in response to bradleyfromjacksonville

I apologize but I don't know Perl language but if you want to be able to read your files you must find a way to match the rule.


Maybe it would be easier to create text files in which values are separated by TAB characters.


Numbers will be perfectly at ease with them (if they are using the .txt name extension) and in this case, no need for single or double quotes.


I wrote quite often that from my point of view, CSV is the worst format ever invented.


Yvan KOENIG (VALLAURIS, France) mercredi 25 janvier 2012

iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.2

My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

Jan 25, 2012 1:04 PM in response to KOENIG Yvan

I would agree there. In other spreadsheet programs you can define the pattern. In those cases since sometimes we have paragraphs in a field that could have any number of symbols I use the pipe symbole | I don't think I have ever seen that used in normal writing. but tabs, commas, periods, symicolons, etc one does see.


I presume that there are no user ways to customize the import process? I looked at preferences and its pretty bare.


Do you know if there is any interest at apple to include features such as custom imports, ODBC, DBI or other database linkages?

Jan 25, 2012 1:07 PM in response to KOENIG Yvan

I Yvan,

I understand your explanation about numbers.

My suggetion was about how to remove the extra commas from a text file (.CSV), that is the commas not to be used to separate the values. May be I forgot to explain to do this with a text editor. After you have got this, you can import the file into numbers and have each field in its cell of numbers. Later you can do any numbers formatting as required.

I believe it will work.

Jan 25, 2012 2:11 PM in response to WALTER-MILANO-ITALY

I apologize but you wrote :



WALTER-MILANO-ITALY wrote:


You should be now able to import the file using ; as separator or if you prefer you can do again the change of ; with ,

It's not a matter of preferences. The OP can't import a file whose values are separated by semi-colons.

He must replace them by commas.

With your scheme :

Original datas


'Total Dollars','$1,123,123,123','some other string','etc'


after your step 1

'Total Dollars';'$1,123,123,123','some other string','etc'


after your step 2

'Total Dollars';'$1,123,123,123''some other string''etc'


and the datas are completly unusable


A possible scheme would be :

Original datas


'Total Dollars','$1,123,123,123','some other string','etc'

'Totals Dollars','$12,123,123,123’,’blahblah’,’etc'


after possible step 1 (replace ‘,’ by ",")

'Total Dollars";"$1,123,123,123";"some other string";"etc'

'Totals Dollars";"$12,123,123,123";"blahblah";"etc'


after possible step 2 (replace single ‘ by ")

"Total Dollars";"$1,123,123,123";"some other string";"etc"

"Totals Dollars";"$12,123,123,123";"blahblah";"etc"


after possible step 3 (replace ";" by ",")

"Total Dollars","$1,123,123,123","some other string","etc"

"Totals Dollars","$12,123,123,123","blahblah","etc"


This one may be imported by Numbers on thez OP’s machine.


I don't know if these changes may be done with Perl but I know that they are easy to do with AppleScript.

But honestly, if Perl is able to build a TAB separated values file, it would be more efficient.


Yvan KOENIG (VALLAURIS, France) mercredi 25 janvier 2012

iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.2

My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do Import a csv file that contains strings

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.