0
$\begingroup$

I created a datafile as follows:

ESTgrowth = FileNameJoin[{"Filepath", "ESTgrowth"}];
open = OpenAppend[ESTgrowth];
Write[open, newdata];  (*"newdata" is a list of data appended to "ESTgrowth" *)
Close[open];

This code appends data stream as follows:

{-17.071, 137.567, 16.554, 0.65}
{-16.873, 136.138, 16.934, 0.7}
{-15.397, 135.738, 16.894, 0.7}
{-14.7, 135.982, 15.9, 0.6}

Then I tried to import the data file to present it using TableForm[...]. Of course,

Import[ESTgrowth, "Table"]

would NOT work because the data stream consists of lists of data only. My question is how to automatically put the data stream into the following format:

{ 
  {-17.071, 137.567, 16.554, 0.65},
  {-16.873, 136.138, 16.934, 0.7},
  {-15.397, 135.738, 16.894, 0.7},
  {-14.7, 135.982, 15.9, 0.6}
}

for TableForm[...] to work as expected.

Screenshot of my dataset with Extra commas placed in fixed positions in each row.

enter image description here

Screenshot of NOT working code.

enter image description here

$\endgroup$
4
  • $\begingroup$ Maybe {Import[...]}? $\endgroup$ Commented May 10, 2020 at 23:12
  • 1
    $\begingroup$ Perhaps study the documentation for Read. Hidden behind Details and Options it claims it can read one Expression. So perhaps Read corresponds to Write in that each can do one Expression at a time. Try it on simple examples and see if you can make it work. $\endgroup$
    – Bill
    Commented May 11, 2020 at 1:17
  • $\begingroup$ Looking at latest screen shot, you do not have an extra "\n" at the front. So you do not need the command In[23356]. Simply do not use that one. I added this since you said you had extra "\n" at the front. Which I also saw on my end when using V 11.3. But it looks like now you do not get that extra "\n". So simply do not use that extra step we added. Try it again, but without step In[23356] where it drops first letter, and it should work. $\endgroup$
    – Nasser
    Commented May 11, 2020 at 19:27
  • $\begingroup$ @Nasser: I will try it again and let you know the result. I hope it will work...Thanks. $\endgroup$ Commented May 11, 2020 at 21:45

2 Answers 2

1
$\begingroup$

You are better off removing the MMA formatting completely, and then relying on ImportString's euristics:

text = Import["yourFile.dat", "Text"];
ImportString@StringReplace[text, {"{" -> "", "}" -> ""}]

(*Out:
{
 {-17.071`, 137.567`, 16.554`, 0.65`},
 {-16.873`, 136.138`, 16.934`, 0.7`},
 {-15.397`, 135.738`, 16.894`, 0.7`},
 {-14.7`, 135.982`, 15.9`, 0.6`}
} 
*)

Really, though, it would be best to plan ahead and write a more sensible format to file (say, a CSV?) in the first place.

$\endgroup$
3
  • $\begingroup$ Before importing my data, I should first export it in text format. I did it following @Nasser's code above and then added ImportString@... as you suggested. But it does not work but creates more problems. $\endgroup$ Commented May 11, 2020 at 0:05
  • 1
    $\begingroup$ @Tugrul I am not sure I understand. I assumed that you had a text file in which the data was presented as you indicated in your question, i.e. rows of expressions like {a, b, c,...}. So I copied the data you had in the question to a file, called it yourFile.dat, then used the code reported. What problems did that create? $\endgroup$
    – MarcoB
    Commented May 11, 2020 at 0:56
  • $\begingroup$ No I did not have a txt file. Every time I do calculations, a list is generated and appended to a file. The subsequent calculations generate similar lists and they are also appended to the file. The file looks like this: {list1} {list2} ...{listn} in the form of multiplication. This is the file I like to import. @Nasser's answer solved my problem with a minor revision. Thank you very much for your time and effort. $\endgroup$ Commented May 11, 2020 at 10:55
1
$\begingroup$

This works on V 12.1 on windows

SetDirectory[NotebookDirectory[]]
open = OpenAppend["ESTgrowth.txt", FormatType -> InputForm];
Do[
  newdata = RandomInteger[{0, 10}, 4];
  Write[open, newdata],
  {n, 1, 3}
  ];
Close[open];

Now the file looks like this

Mathematica graphics

To read it back, we must make a list of lists. This is done by replacing each "\n" with "," and adding "{" at start and adding "}" at end of the string. Only after that, we can convert it to Mathematica experssion.

dataBack = Import["ESTgrowth.txt"];
dataBack = StringReplace[dataBack, "\n" -> ","];
dataBack = StringInsert[dataBack, "{", 1];
dataBack = StringInsert[dataBack, "}", -1];
dataBack = ToExpression[dataBack]

(*    {{3, 9, 1, 2}, {8, 10, 10, 10}, {0, 2, 0, 5}}  *)

If we now run the above code all over again, it still works, and the file now looks like this

Mathematica graphics

And data is read back OK as

  {{3, 9, 1, 2}, {8, 10, 10, 10}, {0, 2, 0, 5}, {0, 7, 5, 6}, {9, 8, 8, 
     8}, {8, 7, 8, 2}}

Update

For V 11.3, it has an extra "\n" at start of file. So for V 11.3 the following should work

open = OpenAppend["ESTgrowth.txt", FormatType -> InputForm];
Do[
  newdata = RandomInteger[{0, 10}, 4];
  Write[open, newdata],
  {n, 1, 3}
  ];
Close[open];

Now

dataBack = Import["ESTgrowth.txt"];
dataBack = StringReplace[dataBack, "\n" -> ","];

(*this line below only needed for V 11.3 *)
dataBack = StringDrop[dataBack, 1]; (*delete the extra "," we added*)

dataBack = StringInsert[dataBack, "{", 1];
dataBack = StringInsert[dataBack, "}", -1];
dataBack = ToExpression[dataBack]

Gives

 {{1, 8, 8, 7}, {0, 7, 8, 0}, {9, 10, 6, 6}}

Update

It looks like OP is till seeing an issue on V 11.3, but I am not able to duplicate it. Here are the steps again showing result of each step. On windows 10.

Here is showing step-by-step the above on V 11.3

Mathematica graphics

open = OpenAppend["ESTgrowth.txt", FormatType -> InputForm];
Do[
  newdata = RandomInteger[{0, 10}, 4];
  Write[open, newdata],
  {n, 1, 3}
  ];
Close[open];

Mathematica graphics

dataBack = Import["ESTgrowth.txt"];
InputForm[dataBack]

Mathematica graphics

dataBack = StringReplace[dataBack, "\n" -> ","];
InputForm[dataBack]

Mathematica graphics

dataBack = StringDrop[dataBack, 1]; (*delete the extra "," we added*)
InputForm[dataBack]

Mathematica graphics

dataBack = StringInsert[dataBack, "{", 1];
InputForm[dataBack]

Mathematica graphics

dataBack = StringInsert[dataBack, "}", -1];
InputForm[dataBack]

Mathematica graphics

dataBack = ToExpression[dataBack];
InputForm[dataBack]

gives

 {{3, 3, 1, 2}, {9, 6, 7, 8}, {6, 1, 10, 6}}
$\endgroup$
21
  • $\begingroup$ My "newdata" is not in the format you have above. My format does not have commas in between individual lists and also the first and the very last curly brackets. This was my problem in the first place. $\endgroup$ Commented May 10, 2020 at 23:42
  • 1
    $\begingroup$ @TugrulTemel My format does not have commas in between individual lists I do not know how this can be possible in Mathematica. You mean like this? list ={ {1} {2}} This becomes multiplication. You can't have list of lists without comma in Mathematica. And I just use the format you showed in your quesiton. But may be your work flow is different. $\endgroup$
    – Nasser
    Commented May 10, 2020 at 23:50
  • $\begingroup$ When I run the model, the output is a list of numbers. This list is saved to ESTgrowth file by OpenAppend[ESTgrowth]. Each time I run the model, the generated list is appended to the ESTgrowth file. Therefore, the final version of ESTgrowth is just a list of lists stacked under each other. MMA does this. $\endgroup$ Commented May 10, 2020 at 23:59
  • $\begingroup$ @TugrulTemel I see. OK, will try to update the answer to reflect this. Just need little more time. Have to make some coeffee $\endgroup$
    – Nasser
    Commented May 11, 2020 at 0:04
  • 1
    $\begingroup$ @TugrulTemel I just tried it on V 11.3 and I do see the difference. But easy to fix. Will post what to do in that case now. $\endgroup$
    – Nasser
    Commented May 11, 2020 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.