I would like to Export a text file, and use paragraph / ENTER commands to place different strings on different lines. Say we have three strings - str1, str2, and str3. How can we place them on separate lines of a text file? This command -

Export["test.txt", StringJoin[str1, str2, str3]];

Places str1, str2, and str3 on a single line.

  • $\begingroup$ Try Export["test.txt",{str1, str2, str3}]. $\endgroup$ – b.gates.you.know.what Sep 15 '13 at 0:04
  • $\begingroup$ @b.gatessucks Works. Terrific. But is there a general way to specify some line depth for each string? $\endgroup$ – DFr Sep 15 '13 at 0:05
  • $\begingroup$ How do you define "line depth"? $\endgroup$ – C. E. Sep 15 '13 at 0:15
  • $\begingroup$ @Anon I define line depth as the line # in a text file. For example "a" [ENTER] "b" [ENTER] [ENTER] "c" would place "c" at a so-called line depth of 4 (since it's four lines down from the top of the file). $\endgroup$ – DFr Sep 15 '13 at 0:18
  • $\begingroup$ OK, just making sure. You can put elements "" instead of strings in the list. E.g. {str1,"","",str2} will make the line depth of str2 be 4. If you say how you intend to indicate on what line a string should appear, such a list could be generated programmatically. $\endgroup$ – C. E. Sep 15 '13 at 0:22

Perhaps something like the following will work for you.

str[1] = "The quick brown fox jumped over the lazy dog";
str[2] = "Grumpy wizards make a toxic brew for the jovial queen";
str[3] = "The five boxing wizards jumped quickly";

text = Normal[SparseArray[{1 -> str[1], 2 -> str[2], 5 -> str[3]}]] /. {0 -> ""}

Export[FileNameJoin[{HomeDirectory[], "Desktop", "test.txt"}], text]

The contents of test.txt will look like this:

The quick brown fox jumped over the lazy dog  
Grumpy wizards make a toxic brew for the jovial queen  

The five boxing wizards jumped quickly


If the strings are indexed by line number, as Yves Klett suggests, then this very simple code will suffice:

str[1] = "line 1"; str[2] = "line 2"; str[5] = "line 5";
text = Table[str[i], {i, 5}] /. str[_] -> ""

Mr.Wizard has come up with an even more concise variant of the above.

str[_] = ""; text = Array[str, 5]

Another edit

Mr.Wizard has also supplied a neat variant of my original SparseArraysolution.

Normal @ SparseArray[{1 -> str[1], 2 -> str[2], 5 -> str[3]}, Automatic, ""]
  • 2
    $\begingroup$ neat use of sparse arrays +1 $\endgroup$ – s0rce Sep 15 '13 at 3:36
  • 1
    $\begingroup$ How about directly using the line number as argument of str? Saves on wear and tear during automation... $\endgroup$ – Yves Klett Sep 15 '13 at 12:17
  • $\begingroup$ @YvesKlett. Good idea. I've edited my answer to incorporate your suggestion. $\endgroup$ – m_goldberg Sep 15 '13 at 16:34
  • 1
    $\begingroup$ I think this can be cleaned up even further: str[_] = ""; text = Array[str, 5] $\endgroup$ – Mr.Wizard Sep 15 '13 at 19:48
  • $\begingroup$ @Mr.Wizard. I take it that your comment is intended to apply only to the case considered int the edit. $\endgroup$ – m_goldberg Sep 16 '13 at 1:11

Alternatively, you can use the newline command "\n" and have the complete file in a single string. For the example above:

str = "The quick brown fox jumped over the lazy dog\nGrumpy wizards make a toxic brew for the jovial queen\n\n\nThe five boxing wizards jumped quickly";
Export["test.txt", str]

gives the text file

enter image description here


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.