5
$\begingroup$

I exported data form Mathematica to txt-format on my Mac. When opening the txt-file on a Windows-System the file does not appear as on my Mac (see screenshots).

Code: Export["Example.txt", {{"Test1", 1, 2, 3, 4}, {"Test2", 5, 6, 7, 8}}]

Mathematica Documentation Center says "Export uses the line separator convention of the computer system on which Mathematica is being run."

How can I generate a txt-file under Mac OS, so that this file can be read correctly under Windows to?

BTW: I would like to get rid of the braces, quotation marks and commas within the txt-file.

enter image description here Mac Screenshot with code

$\endgroup$
4
  • $\begingroup$ Have you tried not using the default OS convention and setting an explicit separator? $\endgroup$
    – rm -rf
    Commented Jun 1, 2012 at 16:51
  • 2
    $\begingroup$ You can also not use notepad and use an editor that will read the file properly like notepad++ $\endgroup$
    – s0rce
    Commented Jun 1, 2012 at 17:01
  • $\begingroup$ or try Export["Example.txt", Compress@{{"Test1", 1, 2, 3, 4}, {"Test2", 5, 6, 7, 8}}] and then Uncompress@Import[...] $\endgroup$
    – acl
    Commented Jun 1, 2012 at 17:05
  • 4
    $\begingroup$ I suspect this is a \n vs. \r\n issue. To solve this and to get rid of the braces, commas and quotation marks you could try doing something like Export["Example.txt", {{"Test1", 1, 2, 3, 4}, {"Test2", 5, 6, 7, 8}}, "Table", "LineSeparators" -> "\r\n"] $\endgroup$
    – Heike
    Commented Jun 1, 2012 at 17:42

2 Answers 2

4
$\begingroup$

To eliminate the braces and quotes, you can use the OutputForm of TableForm like this:

testtext = {{"Test1", 1, 2, 3, 4}, {"Test2", 5, 6, 7, 8}}

Export["text.txt", OutputForm[TableForm[testtext]]]

However, this puts a blank line in between rows.

Heike's suggestion in comments also works, and does not put a blank line between rows, but I would suggest adding a CharacterEncoding option if you are sure you will only use the result on a Windows machine.

Export["text.txt", testtext, "Table", 
 CharacterEncoding -> "WindowsANSI", "LineSeparators" -> "\r\n"]
$\endgroup$
0
5
$\begingroup$

The reason for this is that Windows/DOS and Unix-based systems (like OS X) use different conventions for encoding newlines.

Windows/DOS use a sequence of two character codes: 13 and 10 (13 representing the cursor or print-head returning to the start of the line and 10 representing advancing to the next line). Unix/OS X use a single code: 10. When you use Windows Notepad to open a text file created on a Mac, it will not display it correctly. The little boxes you see represent character code 10, but Notepad never sees a full 13 10 sequence that it can interpret as a newline.

So the problem is not specific to Mathematica. I would suggest that you use a different text editor than Notepad (for example Notepad++), as today nearly all of them support both types of line endings.


Regarding exporting to a table-like format (with no braces), use the Table export format:

Export["Example.txt", {{"Test1", 1, 2, 3, 4}, {"Test2", 5, 6, 7, 8}}, "Table"]
$\endgroup$

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.