MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Reading csv with ;

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99533] Re: [mg99493] Re: Reading csv with ;
  • From: George Woodrow III <georgevw3 at mac.com>
  • Date: Thu, 7 May 2009 06:36:57 -0400 (EDT)
  • References: <gtp1h5$k58$1@smc.vnet.net> <200905060929.FAA02205@smc.vnet.net>

A better way to read in text data is to use ReadList[] instead of 
Import[].

It is better documented as well.

I made a test file:

1;2;3;4;5;6
7;8;9;10;11;12

FilePrint["tempList.txt"]
1;2;3;4;5;6
7;8;9;10;11;12



The lines are terminated with carriage returns, and there is an extra 
carriage return at the end.

Here is what happens with Import[]:

In[33]:= Import["tempList.txt", "Table",   "FieldSeparators" -> ";"]
Out[33]= {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {}}


Here is what happens with ReadList[]:

In[24]:= ReadList["tempList.txt", Word, WordSeparators -> ";"]
Out[24]= {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}

You can preserve the records like this:
In[38]:= ReadList["tempList.txt", {Word, Word, Word, Word, Word, Word},
  WordSeparators -> ";"]
Out[38]= {{"1", "2", "3", "4", "5", "6"}, {"7", "8", "9", "10", "11", 
"12"}}

You can convert the strings to numbers by wrapping ToExpression[] 
around it.

In[39]:= ToExpression[
  ReadList["tempList.txt", {Word, Word, Word, Word, Word, Word},
   WordSeparators -> ";"]]
Out[39]= {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}

Note that in the last three examples, the extra carriage return is lost.

Here is a final way to do it:

In[52]:= Partition[ToExpression[StringSplit[Import["tempList.txt"], 
{";", "\n"}]], 6]
Out[52]= {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}}




Using Import[] is more elegant here. It is too bad that 
"FieldSeparators" is so poorly documented.

As a final note, just incase someone from Wolfram is paying attention:

? *Separator*
System`
NumberSeparator	VerticalSeparator	$PathnameSeparator
RecordSeparators	WordSeparators=09



I think that this command should at least list FieldSeparators. It 
does not.

george woodrow




On May 6, 2009, at 5:29 AM, dh wrote:

>
>
> Hi Dennis,
>
> assume we have a file:
>
> 1;2
>
> 3;4
>
> we can read this by e.g.:
>
>
>
> Import["d:/tmp/t.txt", "Table", "FieldSeparators" -> {";"}]
>
>
>
> The magic word is "FieldSeparators".
>
> Daniel
>
>
>
> Dennis wrote:
>
>> I'm trying to read a csv with mathematica
>
>>
>
>> Now the problem is that my system settings are at ";" as seperator 
>> instead of more traditional ",". So the it effectively looks like 
>> 1;1;1;1 instead of 1,1,1,1
>
>>
>
>> Normally this makes not difference as in most software you can 
>> choose the delimiter/seperator before reading a file, so you pick 
>> =93separator = ;=94 or whatever.
>
>>
>
>> With mathematica however I have so far been unable to do this. Now 
>> the question of course: how so I change the seperator with 
>> mathematica?
>
>>
>
>> Because quite a few files have a =93;=94 separator I would like to find 
>> a solution within mathematica instead of changing the system 
>> settings.
>
>>
>
>> Cheers,
>
>>
>
>> Dennis
>
>>
>
>
>



  • Prev by Date: Re: Re: Solving the system with inexact coefficients
  • Next by Date: Re: Picking Off Lists That Have No Numbers
  • Previous by thread: Re: Re: Reading csv with ;
  • Next by thread: Re: Reading csv with ;