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

share|improve this question
    
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. –  Misery Apr 27 at 13:34
    
it works on mac-os with 'Mathematica 9.0' –  chris Apr 27 at 13:37
    
As usual... make difficult things in Mathematica easily, be unable to make simpliest things. Lol –  Misery Apr 27 at 13:42
    
also works in mathematica 8.0 and 7.0 –  chris Apr 27 at 13:43
    
I use Mathematica 9 on Windows 7 and 8. It works as mentioned in post. –  Misery Apr 27 at 13:44

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer
    
It saves values in braces {} to a file: {x,y} –  Misery Apr 27 at 13:35

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)

share|improve this answer
    
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 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 at 17:42
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.

share|improve this answer
    
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 at 15:44
    
@misery Just put it in the dat list above. It gets exported without quotes. –  Sjoerd C. de Vries Apr 27 at 16:33
    
@SjoerdC.deVries do you understand why my answer not work in windows as claimed? –  chris Apr 27 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 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 at 19:38

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.