0
$\begingroup$

I would like to know how I can transform a file (.dat) that contains

{x1,x2, x3, x4, 
 x5, x6, x7, x8}
{y1,y2, y3, y4, 
 y5, y6, y7, y8}

in another file (.dat) that contains

x1 x2 x3 x4 x5 x6 x7 x8
y1 y2 y3 y4 y5 y6 y7 y8
$\endgroup$
  • 1
    $\begingroup$ Does the existing file look exactly the way you show it? That is, does its 1st line have linefeed character after rightmost comma and does the 2nd line begins a single space? $\endgroup$ – m_goldberg Sep 13 at 18:53
  • $\begingroup$ Can you just do Export[secondFile, Import[firstFile, "Package"], "Table"]? $\endgroup$ – b3m2a1 Sep 13 at 19:22
3
$\begingroup$

Here is one option using text manipulations.

data = Import["test.dat", "Text"]

"{x1,x2, x3, x4, x5, x6, x7,x8} {y1,y2, y3, y4, y5, y6, y7,y8}"

data2 = "{" <> StringReplace[StringReplace[data, WhitespaceCharacter -> ""], 
   "}{" -> "},{"] <> "}"

"{{x1,x2,x3,x4,x5,x6,x7,x8},{y1,y2,y3,y4,y5,y6,y7,y8}}"

Export["test.txt", ToExpression[data2], "Table"];

The resulting text file looks like this: enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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