MathGroup Archive 2011

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

Search the Archive

Re: Prepending Data to a File

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116917] Re: Prepending Data to a File
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Fri, 4 Mar 2011 03:40:26 -0500 (EST)
  • References: <ikihb6$7mh$1@smc.vnet.net>

On 01/03/2011 10:21, Gregory Lypny wrote:
> Hello everyone,
>
> I have a big multi-line, comma-delimited data file.  How would I prepend
> new data to it, that is, insert new lines, generated in Mathematica, at
> the beginning of the file without importing the file into Mathematica?
>
> Suppose my file myData.csv contains
>
> 31,32,33
> 41,42,43
> 51,52,53
>
> and I've now generated these two new lines (lists) in Mathematica
>
> 11,12,13
> 21,22,23
>
> I'd like to prepend those two lines to myData and end up with
>
> 11,12,13
> 21,22,23
> 31,32,33
> 41,42,43
> 51,52,53
>
> I'm guessing that the Write function would do the trick, but I'm not
> sure about the syntax.
>
> Regards,
>
> Gregory
>
>
I don't think there is any solution to this which does not amount to 
creating a new file with the data to be prepended, and then appending 
the other data to it, possibly followed by erasing the original file and 
renaming the new file. This obviously gets expensive if the file is very 
large.

The problem is that operating systems don't usually offer a way to 
perform prepending to files, so neither do computer languages.

Have you thought about storing the file in reverse order, so that you 
can simply append to it, and then reversing the whole file again if 
necessary?

It may be worth telling us a little more about your problem.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: Patterns with conditions
  • Next by Date: Re: Patterns with conditions
  • Previous by thread: Re: Prepending Data to a File
  • Next by thread: Re: Prepending Data to a File