4
$\begingroup$

I know that new line is classically made by "\n".

I want to make a string with lines of data and then export it into a file:

outString = StringJoin["First Line \nSecond Line"]
Put[outString, NotebookDirectory[] <> "testOut.txt"]

The first line, make a decent output in the Mathematica itself:

"First Line 
Second Line"

But in the testOut.txt file , it is :

"First Line \nSecond Line"

Note that it even includes the quotation marks and both in the same line ! What is the solution for that ?

$\endgroup$
6
$\begingroup$
Put[OutputForm[outString], "testOut.txt"]

enter image description here

compare with

Put[outString, "testOut0.txt"]

enter image description here

Alternatively, you can use Export:

Export["testOut2.txt", outString]
(* or Export["testOut2.txt", outString, "Text"] *)

or, WriteStream (thanks: Mr.Wizard)

strm = OpenWrite["testOut2.txt"];
WriteString[strm, outString]
Close[strm]

to get the same result:

enter image description here

$\endgroup$
  • $\begingroup$ Consider also mentioning WriteString +1 $\endgroup$ – Mr.Wizard Apr 23 '15 at 23:31
  • $\begingroup$ Thanks @Mr.W, added WriteStream. $\endgroup$ – kglr Apr 23 '15 at 23:46
  • $\begingroup$ Great ! Thank you $\endgroup$ – Aug Apr 23 '15 at 23:59

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.