2
$\begingroup$

I am going to export some random lists as

Do[

  list = RandomReal[{0, 1}, {10}];

   Export["list-" <> ToString[i] <> "-" <> ToString[i + 2] <> ".txt", 
   list, "Table"];

   , {i, 1, 10}]

The results are as text files with the below format:

for example:

(*list-1-3.txt*)
0.7100544619837812
0.3608062869744495
0.22621774273894735
0.042131840502054985
0.6161812724281575
0.6822336053507754
0.8872971244858767
0.9205321314901684
0.5655929551228038
0.5759357721490808

One column that in each row of that there are many digits. I wish to have One column but less digits: For example:

(*list-1-3.txt*)
0.710054
0.360806
0.226217
0.042131
0.616181
0.682233
0.887297
0.920532
0.565592
0.575935

I used

Do[

  list = RandomReal[{0, 1}, {10}];

   Export["list-" <> ToString[i] <> "-" <> ToString[i + 2] <> ".txt", 
   NumberForm[list, 6], "Table"];

   , {i, 1, 10}]

But the results are as

{0.191954, 0.417033, 0.2569, 0.0891225, 0.242713, 0.614377, 0.32336,   0.803816, 0.24045, 0.741295}

These results are not desired they should be as a one column and without {}. I tried Transpose but it did not work correctly!

$\endgroup$
3
$\begingroup$
paths =
  Table[
   FileNameJoin[{$HomeDirectory, "Desktop", 
     "list-" <> ToString[i] <> "-" <> ToString[i + 2] <> ".txt"}],
   {i, 2}];

Make the files.

SeedRandom[42];
Do[
  list = RandomReal[{-1, 1} {10}];
  Export[
    paths[[i]], 
    (NumberForm[#, {5, 4}, NumberPadding -> {" ", "0"}] // ToString) & /@ list], 
  {i, 2}]

Read in the files and display them.

Multicolumn[Import /@ paths, Frame -> All]

files

What should be noted here is

  • NumberForm is a wrapper that must be supplied to each number individually.
  • The NumberForm wrapped items must be converted strings before exporting.
  • Reading back the files as done above brings the contents of each file as a string in which strings representing the numbers are separated by newling characters (\n)
| improve this answer | |
$\endgroup$
  • $\begingroup$ I am so grateful of you for your answer, BUT recently I have noticed that there has remained a problem: NumberForm[0.00123456 , 4]=0.001234 and NumberForm[0.123456 , 4]=0.1234. The last case is desired but for the first one I wish to have : 0.0012, I think the NumberForm is not a correct function we are searching $\endgroup$ – Unbelievable Jun 30 '17 at 8:06
  • $\begingroup$ @Irreversible. I have fixed a typo in the code for making the files. Perhaps that will clear up the problem. $\endgroup$ – m_goldberg Jun 30 '17 at 8:16
  • $\begingroup$ I had noticed that problem it was about path. But I did not have a problem with that. I have a problem with NumberForm. The @andre asnswer does not have the problem but it has a problem with minus numbers. For example with RandomReal[{-1,1}]!!!! $\endgroup$ – Unbelievable Jun 30 '17 at 8:19
  • $\begingroup$ @Irreversible. I have changed the number specification to {6, 4}. I think it will give you what you want. $\endgroup$ – m_goldberg Jun 30 '17 at 8:26
  • $\begingroup$ @Irreversible. Made another change to address negative number issue. $\endgroup$ – m_goldberg Jun 30 '17 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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