Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
s = OpenWrite[ofile, CharacterEncoding -> "UTF8"]
Export[s, "∂f/∂t"]

did not successfully write into the file. Instead, it writes the InputForm. How to let it write ?

share|improve this question
AFAIK Export and OpenWrite have no relationship whatsoever. I find it a small wonder that you can even use a file handle in Export. – Sjoerd C. de Vries Sep 9 '12 at 22:40
Really? No wonder. My guess is Export[file, ...] internally will create file handle any way and using file handle directly probably just mean a replacement didn't occur. It might be an accident due to implementation details. – qazwsx Sep 9 '12 at 23:03
up vote 7 down vote accepted

F'x is correct. Using WriteString is the method to use. If you want to write a "big expression", try the following.

Let's assume a large matrix with dimensions of 100 by 5;

a = RandomReal[{1,10},{100,5}]

We can easily write this using the WriteString method.

s = OpenWrite["~/Desktop/toto.txt", CharacterEncoding -> "UTF8"]
WriteString[s, a]

This will write to the text file exactly as you see it printed in the notebook interface.

enter image description here

You can also print in Grid or TableForm

a = RandomReal[{1,10},{100,5}]//TableForm

enter image description here

You can also get a little crazy and riffle in delimiters...

a = "a =   "<> StringJoin[Riffle[ToString[#^2] & /@ Range[10], "\t"]]

enter image description here

Also, don't forget the OpenAppend function to write data at the end of a file without overwriting existing info.

b = "b =   " <> StringJoin[Riffle[ToString[#^3] & /@ Range[10], "\t"]]
s = OpenAppend["~/Desktop/toto.txt", CharacterEncoding -> "UTF8"]
WriteString[s, "\n"]
WriteString[s, b]
share|improve this answer


s = OpenWrite["~/Desktop/toto.txt", CharacterEncoding -> "UTF8"]
WriteString[s, "\[PartialD]f/\[PartialD]t"]
share|improve this answer
But I have a big expression to write. Not just a string. – qazwsx Sep 9 '12 at 21:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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