I have two lists:

{a, 1, b} and {b, 1.3, y}

My goal is to print them to file test.dat, so that the content of test.dat looks like:

(a 1 b)
(b 1.300 y)

I want the elements of the lists in the output file to be separated by spaces.


  1. How can I do it?
  2. Is it possible to use Print command to do it?

Edit 1:

I was going along these lines:

myPrint[list_] := Print["(", Row[list, " "], ")"]
l1 = {a, 1, b};

(a 1 b)

My problem is that I don't know how to export the results I got to a file. So this is the reason for my question 2.

Edit Final: Problem Solved

Solution by swish (from comments)

str = OpenWrite["test.dat"]
myPrint[list_] :=
  WriteString[str, Row[{"(", Row[NumberForm[#, {4, 3}] & /@ list, " "], ")\n"}]]
l1 = {a, 1, b}
  • $\begingroup$ You've seen Export[]? $\endgroup$ – J. M. is away Apr 18 '13 at 11:05
  • $\begingroup$ @J.M., thank you for your comment. Yes, I did, but it has some predefined format of output (for example, column output - each element of the list is being output on a new line). What I want is control over the output format. I got the idea how to get this control using Print function, but have no clue how to output (Export?) results of the Print function to file. $\endgroup$ – user1541776 Apr 18 '13 at 15:12

Some tweaks to your function and you done:

myPrint[list_] := 
 ToString[Row[{"(", Row[NumberForm[#, {4, 3}] & /@ list, " "], ")"}]]

Now it returns string and you can Export it.

  • $\begingroup$ thank you very much! This solves my problem! Still, I've got one more question: I read that Print outputs to \$Output. It seems that if I set \$Output to some file, Print will do the job without necessity to transform output into String. Does anyone know how to set \$Output to output to file test.dat? $\endgroup$ – user1541776 Apr 18 '13 at 15:37
  • $\begingroup$ @user1541776 Use OpenWrite to open your file and Write to it instead of Print. $\endgroup$ – swish Apr 18 '13 at 15:46
  • $\begingroup$ thank you once again! The problem is that it outputs into file "Row[{"(", Row[{NumberForm[a, {4, 3}], NumberForm[1, {4, 3}], NumberForm[b, {4, 3}]}, " "], ")"}]" rather than (a 1 b), i.e. in some sense outputs the expression and not the result of the evaluation of expression. (Tried to add Evaluat[] - it didn't help). $\endgroup$ – user1541776 Apr 18 '13 at 15:53
  • $\begingroup$ @user1541776 WriteString then :). $\endgroup$ – swish Apr 18 '13 at 15:55
  • $\begingroup$ thank you, it worked! You made my day :-) $\endgroup$ – user1541776 Apr 18 '13 at 17:26

Here's a simple and inelegant way. Define a function that places the information in the desired text-string form.

string[x_]:="("~~ToString[x[[1]]]~~" "~~ToString[x[[2]]]~~" "~~ToString[x[[3]]] ~~ ")"

and then Export

 Export["test.txt", {string[x], string[z]}]


x = {a, 1, b}
z = {b, 1.3, y}
  • $\begingroup$ thank you for your answer. There are two things I need: 1. I would like to print lists with arbitrary number of elements (not necessarily 3 as I specified in my example) 2. I would like to have more control over printed numbers - floats to be output in %f4.3 format rather than %f2.1. $\endgroup$ – user1541776 Apr 18 '13 at 15:04

A solution based on StringReplace and Export and with number format control:

x = {a, 1, b}
z = {b, 1.3, y, 4}

(* replace brackets and braces *)
   sr[x_] := StringReplace[
  ToString@(x /. y_?NumericQ -> NumberForm[y, {3, 4}]), {","->" ", "{"->"(", "}"->")"}]

(* Export a collection of your expressions to a file *)
Export["/tmp/test.tmp", sr /@ {x, z, x, z}, "Table"]

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.