2
$\begingroup$

I need to make a bunch of plaintext files, all of which differ only in a single parameter. The text I want to "copy" or "batch produce" is a little bit complex, since it contains quoted areas and greek letters that I want to be output in their \[letter] format. What I would like to do is to devise a code in which I can input, say,

My house number i is "my house" number i, letter α

And, when run from i=1 to i=4 output four different plaintext files containing

My house number 1 is "my house" number 1, letter \[Alpha]

My house number 2 is "my house" number 2, letter \[Alpha]

My house number 3 is "my house" number 3, letter \[Alpha]

My house number 4 is "my house" number 4, letter \[Alpha]

respectively (i.e, the first output file the first phrase, the second file the second phrase, and so on.)

Jason's advice is very insightful, but I'm having problems with the quotations still. Is there any way I can do what I want to do? If so, how?

Thank you so much for your help

$\endgroup$
6
  • $\begingroup$ Put may be useful here. $\endgroup$
    – Yves Klett
    Apr 29, 2016 at 7:26
  • 1
    $\begingroup$ Do[ Export["file_" <> IntegerString[n] <> ".txt", "My house number " <> IntegerString[n] <> " is \"my house\" number " <> IntegerString[n]], {n, 5}] $\endgroup$
    – Jason B.
    Apr 29, 2016 at 9:44
  • $\begingroup$ Do[Export["file_" <> IntegerString[n] <> ".txt", "My house number " <> IntegerString[n] <> " is \"my house\" number " <> IntegerString[n] <> " letter \[Alpha]", CharacterEncoding -> "ASCII"], {n, 5}] $\endgroup$
    – Jason B.
    Apr 29, 2016 at 10:16
  • $\begingroup$ Any more changes or can I put this in the answer? :-D $\endgroup$
    – Jason B.
    Apr 29, 2016 at 10:16
  • $\begingroup$ This works, thank you for your time and apologies for the misunderstandings $\endgroup$
    – Alex
    Apr 29, 2016 at 10:39

1 Answer 1

5
$\begingroup$

There are two issues here:

  1. How to write a string that contains a quotation within it, when the quotation mark is what signifies to the system when a string begins and ends.
  2. How to export strings with special characters, and have them written in their escaped form: \[Alpha] and not α

For the first, you need to construct the string using \" to begin and end the quotation within the string. For the second you need to export using ASCII character encoding, rather than the more modern UTF-8.

This should create the files you describe:

Do[Export["file_" <> IntegerString[n] <> ".txt", 
  "My house number " <> IntegerString[n] <> 
   " is \"my house\" number " <> IntegerString[n] <> 
   " letter α", CharacterEncoding -> "ASCII"], {n, 5}]

Old Answer

Use Write instead of Export

This command

Write["test.txt", "Hello α world \n Goodbye β world"]

produces this output in the file,

"Hello \[Alpha] world \n Goodbye \[Beta] world"

but this command

Export["test2.txt", "Hello α world \n Goodbye β world"]

give this file

Hello α world

Goodbye β world

$\endgroup$
4
  • $\begingroup$ Hi Jason, thank you for the very nice and elegant answer. However, I'm having issues when trying to save outputs with non-quoted parts, such as Hello ''World''. Do you know how to have this exact string in the output file? $\endgroup$
    – Alex
    Apr 29, 2016 at 8:58
  • $\begingroup$ Alex, you need to make your question clear from the beginning, to avoid having answers that, while they answer the exact question in the post, don't answer your real problem. In the code you posted, you were writing a string to a file, and you said you wanted to keep the quotes in the string and to keep the special characters in their escaped form. But what do you mean by Hello "World" - is that a string? If not, then Mathematica interprets it as Times[ "World", Hello] $\endgroup$
    – Jason B.
    Apr 29, 2016 at 9:06
  • $\begingroup$ I apologize that the fact that the quotations did not cover the whole text was not clear enough. I have rewritten the question to make more explicit the exact thing that I want $\endgroup$
    – Alex
    Apr 29, 2016 at 9:19
  • $\begingroup$ On a tiny side annotation, if you want UNIX-type newlines instead of Windows-type, one needs to add the option DOSTextFormat -> False to the Export command. $\endgroup$
    – Alex
    May 1, 2016 at 21:21

Your Answer

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

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