Re: Bug in Import?
- To: mathgroup at smc.vnet.net
- Subject: [mg54761] Re: [mg54739] Bug in Import?
- From: "Christoph Lhotka" <lhotka at astro.univie.ac.at>
- Date: Tue, 1 Mar 2005 01:58:16 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On Mon, 28 Feb 2005 03:27:49 -0500 (EST)
Mark Fisher <mark at markfisher.net> wrote:
> (1) I export a data set that has two identitcal rows.
> (2) I use "!!" to display the stored file, showing that it contains two
> identical rows.
> (3) I import the file, the result of which does *not* have two identical
> rows: The first row is different from the second row.
>
> Export["test.tsv",
> {{"2005-01-24", "2005-02-14"},
> { "2005-01-24", "2005-02-14"}}]
>
> !!test.tsv -->
>
> 2005-01-24 2005-02-14
> 2005-01-24 2005-02-14
>
> Import["test.tsv"] -->
>
> {{2005-01-24, 2005-02-14},
> {2005, -1, -24, 2005, -2, -14}}
>
> Surely this is a bug.
>
> (I sent this example to support at wolfram.com three weeks ago, but the
> only response I've received is that it's not a bug. Frankly, I don't
> think the tech person who responded understood the example.)
>
> I am using version 5.1 for Windows.
>
> --Mark.
>
Hi !
I am not sure, if it is a bug, but you are right, it looks strange. If you
want to have more control over, how data is stored and recieved use the
functions Write and ReadList :
You open an outputstream with OpenWrite
In[..]:=stream = OpenWrite["test.tsv"]
Out[..]:=OutputStream[test.tsv, 113]
and write something to the stream with Write (or WriteString, see the help
browser):
In[..]:=Write[stream,
{{"2005-01-24", "2005-02-14"}, {"2005-01-24", "2005-02-14"},
{"2005-01-24", "2005-02-14"}} // TableForm // StandardForm]
Out[..]:=test.tsv
Instead of Import, you can use ReadList (or Read, to import not the whole
file):
In[..]:=ReadList["test.tsv", {"Word", "Word"}]
Out[..]:={{2005-01-24, 2005-02-14}, {2005-01-24, 2005-02-14}, {2005-01-24,
2005-02-14}}
(where you can explicitly specify, how the data should be treated
("Word","Real","Record",etc...} and the inner structure in form of nested
lists.
So in your example {"Word","Word"} means 2 colums of type Word, which tells
Mathematica to import strings into a list of form
{{word1,word2},{word3,word4},...}
In[..]:=FullForm[%]
Out[..]:=List[List["2005-01-24", "2005-02-14"], List["2005-01-24",
"2005-02-14"],
List["2005-01-24", "2005-02-14"]]
Don't forget to close the output / input stream with Close after use:
In[..]:=Close[stream]
...
-- Christoph Lhotka --
University of Vienna
Institute for Astronomy
Tuerkenschanzstr. 17
1180 Vienna, Austria
fon. +43.1.4277.518.41
mail. lhotka at astro.univie.ac.at
- Follow-Ups:
- Re: Re: Bug in Import?
- From: Chris Chiasson <chris.chiasson@gmail.com>
- Re: Re: Bug in Import?