| Author |
Comment/Response |
Forum Moderator
email me
 |
09/18/07 5:17pm
As far as I know, if there is no separator for the blank data in the data file, you don't have much hope.
If however, there is a separator, e.g. here is a space delimited (delimited = separated) file with two spaces between the x and z
In[61]:= FilePrint["C:\\Documents and Settings\\default\\Desktop\\abcxz.txt"]
During evaluation of In[61]:=
a b c
x z
Import still has trouble:
In[62]:= Import["C:\\Documents and Settings\\default\\Desktop\\abcxz.txt", "Table"]
Out[62]= {{"a", "b", "c"}, {"x", "z"}}
(* There might be something clever to get Import to do this but I can't think of it off hand. Perhaps someone else can *)
But ReadList with some options will do the trick:
In[63]:= ReadList["C:\\Documents and Settings\\default\\Desktop\\abcxz.txt", Word,
WordSeparators -> {" "}, RecordLists -> True, NullWords -> True]
Out[63]= {{"a", "b", "c"}, {"x", "", "z"}}
Here is a Tab delimited file with two tabs between the x and z.
In[75]:= FilePrint["C:\\Documents and Settings\\default\\Desktop\\abcxzTabs.txt"]
During evaluation of In[75]:=
a b c
x z
Still Import has trouble with Table:
In[76]:= Import["C:\\Documents and Settings\\default\\Desktop\\abcxzTabs.txt", "Table"]
Out[76]= {{"a", "b", "c"}, {"x", "z"}}
But not "TSV" (tab separated values")
In[77]:= Import["C:\\Documents and \
Settings\\default\\Desktop\\abcxzTabs.txt", "TSV"]
Out[77]= {{"a", "b", "c"}, {"x", "", "z"}}
and ReadList with options works:
In[78]:= ReadList["C:\\Documents and Settings\\default\\Desktop\\abcxzTabs.txt", Word,
WordSeparators -> {"\t"}, RecordLists -> True, NullWords -> True]
Out[78]= {{"a", "b", "c"}, {"x", "", "z"}}
Finally you could have a comma delimited file:
In[85]:= FilePrint["C:\\Documents and Settings\\default\\Desktop\\abcxz.csv"]
During evaluation of In[85]:=
a,b,c
x,,z
In[88]:= Import["C:\\Documents and \
Settings\\default\\Desktop\\abcxz.csv", "CSV"]
Out[88]= {{"a", "b", "c"}, {"x", "", "z"}}
Note that all of your list entries are strings. you may need to apply ToExpression to the lists to get symbols and/or numbers:
In[89]:= ToExpression[
Import["C:\\Documents and Settings\\default\\Desktop\\abcxz.csv",
"CSV"]]
Out[89]= {{a, b, c}, {x, Null, z}}
All this is done with V6. All of the above should work except using FilePrint to view the files in V5. Something like
!!"C:\\Documents and Settings\\default\\Desktop\\abcxz.csv"
should work in most V5 installations.
Tom Zeller
URL: , |
|