This question already has an answer here:

I want to use a Python software which reads data in columns from an ASCII File. The Problem is that each new line shoud be seperated only by "\n" and not by "\n\r" as in files which Export[filename, data, "Table"]does.

Question: Is it possible to Export data with Mathematica by specifying that only "\n" should be used at end of each line?


marked as duplicate by glS, Community Mar 20 '17 at 10:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @:glS, Community: Thank you for the link to the duplicate question. $\endgroup$ – mrz Mar 20 '17 at 10:49

You can use OpenWrite in such cases.

data = RandomReal[1, {5, 3}];

f = OpenWrite["data.dat"];

 WriteString[f, StringJoin[{ToString[#], "  "} & /@ data[[n]], "\n"]]
 , {n, Length[data]}]

Usually, I use it when I have a long cycle and I want my data to be written in each cycle. So I keep the Do/Table structure.

You can further use NumberForm or any format or delimiter (I use " " here) with your data which makes a nice Fortran/Python looking data table.

  • $\begingroup$ I used a hex editor (HxD for Windows) and looked into the file data.dat. Each line still ends with 0D0A which is \n\r. $\endgroup$ – mrz Mar 20 '17 at 10:46
  • $\begingroup$ Try WriteString[f, StringJoin@Table[{{ToString[#], " "} & /@ data[[n]], "\n"} , {n, Length[data]}]] $\endgroup$ – Sumit Mar 20 '17 at 11:10

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