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 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

share|improve this question
    
Have you tried not using the default OS convention and setting an explicit separator? –  rm -rf Jun 1 '12 at 16:51
2  
You can also not use notepad and use an editor that will read the file properly like notepad++ –  s0rce Jun 1 '12 at 17:01
    
or try Export["Example.txt", Compress@{{"Test1", 1, 2, 3, 4}, {"Test2", 5, 6, 7, 8}}] and then Uncompress@Import[...] –  acl Jun 1 '12 at 17:05
4  
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"] –  Heike Jun 1 '12 at 17:42

2 Answers 2

up vote 3 down vote accepted

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

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"]
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.