Export, adding / preliminary preparation of data and text to files (Mathematica)

I export the data from the table "mydata1" in CSV format to the file "file1.dat". The following is the math code:

mydata1=TableForm[Flatten[ Table[Table[Table[ {xcord, ycord, zcord}, {xcord, 0,50,10}], {ycord,0,50,10}], {zcord, 50, 100, 10}], 2]]; Export["file1.dat",mydata1,"CSV"] 

Now my file1.dat file looks like this:

 0,0,50 10,0,50 20,0,50 .. .. and so on 

Now I have another dataset from the table "mydata2" (the code below). I want to save the data from this table "mydata2" into one file "file1.dat". But before I do this I need to write the text in the file "file1.dat", for example, "The data below is from mydata2".

Pay attention to both data from both tables that need to be exported in CSV format.

 mycounter=20 mydata2=TableForm[Flatten[ Table[Table[Table[ {++mycounter,xcord, ycord, zcord}, {xcord, 0,50,10}], {ycord,0,50,10}], {zcord, 50, 100, 10}], 2]]; 

at the end, my data file "file1.dat" should look like this:

 *Data from data from mydata1 0,0,50 10,0,50 20,0,50 ... and so on *Below data from mydata2 21,0,0,50 22,10,0,50 23,20,0,50 ... and so on. 

If you observe the final data file, "file1.dat" should have data from the table "mydata2" below the data from "mydata1", and there is written text between them.

Note. I am ready to export data with TXT extension, but in CSV format. For instance:

 Export["file1.txt", mydata1, "CSV"] 

I used "PutAppend", but it did not give me the desired results. Either I am not using it correctly, or perhaps this is not a keyword for my problem.

I have many questions regarding exports, but I would not ask for it now, since I do not want to confuse all of you.

+6
source share
2 answers

Perhaps something like:

 mydata1 = Flatten[Table[Table[Table[ {xcord, ycord, zcord}, {xcord, 0, 20, 10}], {ycord, 0, 20, 10}], {zcord, 50, 50, 10}], 2]; mycounter = 20 mydata2 = Flatten[Table[Table[Table[ {++mycounter, xcord, ycord, zcord}, {xcord, 0, 20, 10}], {ycord, 0, 20, 10}], {zcord, 50, 50, 10}], 2]; Export["c:\\test.txt", Join[{"* data1 follows"}, mydata1, {"* data2 follows"}, mydata2], "CSV"] 

The resulting file:

 * data1 follows 0,0,50 10,0,50 20,0,50 0,10,50 10,10,50 20,10,50 0,20,50 10,20,50 20,20,50 * data2 follows 21,0,0,50 22,10,0,50 23,20,0,50 24,0,10,50 25,10,10,50 26,20,10,50 27,0,20,50 28,10,20,50 29,20,20,50 
+3
source

Threads are useful for this purpose. If your goal is to generate mydata1 and mydata2 at different times, you can do the following. First create a file containing mydata1 :

 $stream = OpenWrite["file.dat", BinaryFormat -> True]; WriteString[$stream, "*Data from data from mydata1\n"] Export[$stream, mydata1, "CSV"] WriteString[$stream, "\n"] Close[$stream] 

We can check the contents of the file using Import :

 In[10]:= Import["file.dat", "Text"] Out[10]= *Data from data from mydata1 0,0,50 10,0,50 20,0,50 ... 

Now add mydata2 to the end of the file:

 $stream = OpenAppend["file.dat", BinaryFormat -> True]; WriteString[$stream, "*Below data from mydata2\n"] Export[$stream, mydata2, "CSV"] WriteString[$stream, "\n"] Close[$stream] 

... and check again:

 In[20]:= Import["file.dat", "Text"] Out[20]= *Data from data from mydata1 0,0,50 10,0,50 20,0,50 ... *Below data from mydata2 21,0,0,50 22,10,0,50 23,20,0,50 ... 

Optionally, you can also write mydata1 and mydata2 to a file:

 $stream = OpenWrite["file.dat", BinaryFormat -> True]; WriteString[$stream, "*Data from data from mydata1\n"] Export[$stream, mydata1, "CSV"] WriteString[$stream, "\n*Below data from mydata2\n"] Export[$stream, mydata2, "CSV"] WriteString[$stream, "\n"] Close[$stream] 

Please note that the BinaryFormat -> True parameter BinaryFormat -> True used every time we open the stream. This disables the automatic insertion of a new line using stream write operations. If this parameter is omitted, an undesirable double interval occurs in the output file.

+10
source

Source: https://habr.com/ru/post/897313/


All Articles