14
$\begingroup$

I am trying to Write String to file, something like this:

Write["test.txt", "This is a test"];

when I opened the file I found that the string was written with the Quotation mark " like this:

"This is a test"

how can I write only the string text?

Note: "This is a test" is part of matrix and I can't manually type it like this:

Write["test.txt", This is a test];
$\endgroup$
4
  • $\begingroup$ Try Export instead of Write. $\endgroup$
    – seismatica
    Jul 16, 2014 at 8:43
  • $\begingroup$ Does not help because Export overwrite the file. I am using continues writing to the file using OpenAppend $\endgroup$ Jul 16, 2014 at 8:48
  • $\begingroup$ Related: (648), (5585), (41047) $\endgroup$
    – Mr.Wizard
    Jul 16, 2014 at 10:12
  • $\begingroup$ Can't you use Export["test.txt","This is a test","Text"] $\endgroup$ Mar 16, 2017 at 18:44

2 Answers 2

14
$\begingroup$

one way:

 SetDirectory[NotebookDirectory[]];
 fileName = "test.txt";
 file = OpenWrite[fileName, PageWidth -> Infinity];
 WriteString[file,"this is a string which will show up without the quotes"];
 Close[file]

Mathematica graphics

To open for append and lines:

SetDirectory[NotebookDirectory[]];
fileName = "AAA.txt";
file = OpenAppend[fileName, PageWidth -> Infinity];
WriteString[file, "this is a string which will show up without the quotes\n"];
WriteString[file, "second line\n"];
Close[file]

Mathematica graphics

important: must add "\n" at the end of the string. Without it, all string will remain on same line.

You can also in the notebook, add the new line, and will be be reflected in the file. Like this:

Mathematica graphics

Notice, in the above there is no explicit "\n" there, but it was added in the notebook. The above will look like this in the text file:

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Great. Thanks a lot. $\endgroup$ Jul 16, 2014 at 9:02
  • $\begingroup$ One thing I want to add, the text I am writing is an element in big list. you would agree if instead of adding "\n" to the end of the text, I can add WriteString["AAA.txt", "\n"]; after what I want to write. $\endgroup$ Jul 16, 2014 at 9:06
  • $\begingroup$ @Algohi First, it is WriteString[file, "\n"] where file is the file handle returned from the OpenAppend call. Second, yes, you can do "\n" on its own, it will go to the end of the last written string and make new line. WriteString does not add "\n" on its own. The user has control on that. You can write WriteString[file,"A\nB\n"] if you want. $\endgroup$
    – Nasser
    Jul 16, 2014 at 9:09
4
$\begingroup$

Addressing the comment, WriteString can take multiple arguments to write multiple expressions so you can do:

 WriteString[file, "string" , "\n" ]

which may be somewhat more convenient than concatenating the "\n" to the string.

If you want to write a list of strings adding linebreaks you can do like this:

 WriteString[f, 
      Sequence @@ Riffle[{"string1", "string2", "String3"}, "\n"], "\n"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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