9
$\begingroup$

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.

$\endgroup$
14
  • $\begingroup$ It won't work. First: arguments are misplaced. Second: it will return values in braces {}. What I am asking: is this "small variation" which I can't figure out. $\endgroup$
    – Misery
    Apr 27, 2014 at 13:34
  • $\begingroup$ it works on mac-os with 'Mathematica 9.0' $\endgroup$
    – chris
    Apr 27, 2014 at 13:37
  • $\begingroup$ As usual... make difficult things in Mathematica easily, be unable to make simpliest things. Lol $\endgroup$
    – Misery
    Apr 27, 2014 at 13:42
  • $\begingroup$ also works in mathematica 8.0 and 7.0 $\endgroup$
    – chris
    Apr 27, 2014 at 13:43
  • 1
    $\begingroup$ I can do this in Python. I would like to have this ultra-mega-incredible feature also in mathematica :] $\endgroup$
    – Misery
    Apr 27, 2014 at 13:48

3 Answers 3

5
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ It saves values in braces {} to a file: {x,y} $\endgroup$
    – Misery
    Apr 27, 2014 at 13:35
21
$\begingroup$
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.

$\endgroup$
5
  • $\begingroup$ How can I insert there also a string so it appears without quote marks? 1. 0.84 0.54 " string" 1.55 $\endgroup$
    – Misery
    Apr 27, 2014 at 15:44
  • $\begingroup$ @misery Just put it in the dat list above. It gets exported without quotes. $\endgroup$ Apr 27, 2014 at 16:33
  • $\begingroup$ @SjoerdC.deVries do you understand why my answer not work in windows as claimed? $\endgroup$
    – chris
    Apr 27, 2014 at 18:22
  • $\begingroup$ @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. $\endgroup$ Apr 27, 2014 at 19:10
  • $\begingroup$ @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? $\endgroup$
    – Mr.Wizard
    Apr 27, 2014 at 19:38
4
$\begingroup$

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)

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Apr 28, 2014 at 17:31
  • $\begingroup$ 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. $\endgroup$
    – george2079
    Apr 28, 2014 at 17:42

Your Answer

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

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