MathGroup Archive 2010

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

Search the Archive

Re: Export

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110152] Re: Export
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Fri, 4 Jun 2010 08:02:40 -0400 (EDT)

On 6/3/10 at 5:38 AM, clint.zeringue at kirtland.af.mil (Clint) wrote:

>How can I accomplish the following:

>mylist = {1,2,3,4};

>I would like to "export" my list to a data file so that inside the
>data file it would look like:

>1,2,3,4 on the first line...

This is easily done using Export. That is

Export[filename, {{1,2,3,4}},"CSV"]

will create the desired file. Note, the double brackets are
needed here. Without them, Export will create 4 rows.

>I then need to be able to read this back in using import..

This is done by doing Import[filename,"CSV"]

>I also need the ability to "Append" to this data file another line
>of data (ie mylist2 = {2,4,6,8})

>I would like to "write" this second line of data to the same file so
>that the new file would look like:

>1,2,3,4 on first line...
>2,4,6,8 on second line... etc..

I would do this as follows:

read in the contents of the existing file using Import[filename,
"CSV"] to a conveniently variable.
append the new data to that variable
export entire data set back out to the file using
Export[filename, variablename, "CSV"]

If there currently is an option to append new data to "CSV"
files I am not aware of it.

It would be possible to create a means to append to "CSV" files
if that is needed due to say a very large file size it could be
done as:

str=OpenAppend[filename];
WriteString[str, "\n",StringTake[ToString@{2,4,6,8}],{2,-2}];
Close[str];

A couple of things here. I've assumed an existing file
(filename) created using Export as above. The last line of a
"CVS" file created by Export won't have a newline and
WriteString does not write a new line. That is why the newline
is written first.

WriteString will convert the data to a string before writing it
out. But with a list that means an set of curly braces
surrounding the output string. That is why the
StringTake[ToString@ part is included.

Finally, this will be a comma separated value format but it will
also have spaces between the values. This won't matter to
Import. But it might for some other purpose.

>Then also how can I access a specific line of data?

This last can be done as

Import[filename, {"Data",n}]

where n is the line number you want to access



  • Prev by Date: Re: Big memory needed
  • Next by Date: Re: discretized Laplacian or linear inverse problem with extremely
  • Previous by thread: Export
  • Next by thread: Re: Export