10

I am preparing data used by a command line program. It requires data in a simpliest possible format:

value_x1 value_y1
value_x2 value_y2
...    
value_xN value_yN

For now I always need to use Python to process a datafile created in Mathematica that format, because Mathematica always adds quotes " or multiple blank spaces or tabs. How can I create a file in a format:

<value_x1><single_space_sign><value_y1><end_of_line_sign>
<value_x2><single_space_sign><value_y2><end_of_line_sign>
...    
<value_xN><single_space_sign><value_yN><end_of_line_sign>

OpenWrite[], Write[], Export[,"Table"] etc. produce files with additional characters, and therefore are useless without processing in Python.

Edit: What if I needed to create a file in a format:

<value_x1><single_space_sign><some string character><value_y1><end_of_line_sign>
<value_x2><single_space_sign><some string character><value_y2><end_of_line_sign>
...    
<value_xN><single_space_sign><some string character><value_yN><end_of_line_sign>

Edit 2: Proposed answer:

dat = Table[{i, Sin[i]}, {i, 4}] // N;
Export["test.dat", dat]

produces file containing following data:

{1., 0.8414709848078965}
{2., 0.9092974268256817}
{3., 0.1411200080598672}
{4., -0.7568024953079282}

Which is useless.

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
Misery
  • 2,640
  • 1
  • 30
  • 39

3 Answers3

22
dat = Table[{i, Sin[i], Cos[i], Tan[i]}, {i, 4}] // N;

Export["test.txt", dat, "Table",  "FieldSeparators" -> " "]

FilePrint["test.txt"]
  1. 0.8414709848078965 0.5403023058681397 1.5574077246549023
  2. 0.9092974268256817 -0.4161468365471424 -2.185039863261519
  3. 0.1411200080598672 -0.9899924966004454 -0.1425465430742778
  4. -0.7568024953079282 -0.6536436208636119 1.1578212823495777

Nothing new here. It's all in the documentation.

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
  • How can I insert there also a string so it appears without quote marks? 1. 0.84 0.54 " string" 1.55 – Misery Apr 27 '14 at 15:44
  • @misery Just put it in the dat list above. It gets exported without quotes. – Sjoerd C. de Vries Apr 27 '14 at 16:33
  • @SjoerdC.deVries do you understand why my answer not work in windows as claimed? – chris Apr 27 '14 at 18:22
  • @chris If I use a.txt extension for the file and your answer I get the behavior Misery reports. If I use .dat as extension one would think it works, but it actually uses tab characters as field separators. My guess is that Misery wrote to a .txt file which yields a different default behavior than to .dat files. By using "Table" as an explicit export format I overruled MMA's format guessing based on extension. – Sjoerd C. de Vries Apr 27 '14 at 19:10
  • @Misery This also works for me in version 7 under Windows 7. If I use e.g. dat = {{1, "foo"}, {2.2, bar}, {Pi, -7.31*^134}} there are no quotation marks around foo in the output. Are you saying this does not work on your system? – Mr.Wizard Apr 27 '14 at 19:38
5

Try

dat = Table[{i, Sin[i]}, {i, 4}] // N;
Export["test.dat", dat]

So that

 FilePrint["test.dat"]

returns

(* 1. 0.8414709848078965 2. 0.9092974268256817 3. 0.1411200080598672 4. -0.7568024953079282 *)

chris
  • 22,860
  • 5
  • 60
  • 149
4

I don't know if this may be the issue, but specifying "FieldSeparators" to a space causes multi-word strings to be quoted:

 ExportString[{{1, 1}, {"dog", "and cat"}}, "Table",  "FieldSeparators" -> " "]

1 1

dog "and cat"

Fix with TextDelimiters

 ExportString[{{1, 1}, {"dog", "and cat"}}, "Table", 
     "FieldSeparators" -> " ", "TextDelimiters" -> ""]

1 1

dog and cat

Obviously you now can't simply read it back in as a table recovering the original strings.

(verifed for export to files under Windows using either a ".dat" extension, or the "Table" argument)

george2079
  • 38,913
  • 1
  • 43
  • 110
  • The quoting of fields that contain a field separator is a standard operating procedure. If this is not done fields cannot be correctly recognized anymore. Try, for instance, saving as CSV an Excel sheet with a text field containing a comma, among other characters. – Sjoerd C. de Vries Apr 28 '14 at 17:31
  • I did find it in the docs: "By default, Export delimits text fields containing the field separator with double-quote characters." I will take that note out. – george2079 Apr 28 '14 at 17:42