Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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];
share|improve this question
    
Try Export instead of Write. –  seismatica Jul 16 at 8:43
    
Does not help because Export overwrite the file. I am using continues writing to the file using OpenAppend –  Algohi Jul 16 at 8:48
    
Related: (648), (5585), (41047) –  Mr.Wizard Jul 16 at 10:12

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
    
Great. Thanks a lot. –  Algohi Jul 16 at 9:02
    
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. –  Algohi Jul 16 at 9:06
    
@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. –  Nasser Jul 16 at 9:09

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"]
share|improve this answer

Your Answer

 
discard

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.