2
$\begingroup$

I have the following list

FirstPart={pr[4, k1, 0] == 1/pr1, pr[4, k2, 0] == 1/pr2, pr[4, k3, 0] == 1/pr3, 
 pr[4, k1 - k2, mZ] == 1/pr4, pr[4, k2 - k3, 0] == 1/pr5, 
 pr[4, k1 - k3, 0] == 1/pr6, pr[4, k1 - p1, 0] == 1/pr7, 
 pr[4, k1 - p1 - p2, 0] == 1/pr8, pr[4, k2 - p1 - p2, 0] == 1/pr9, 
 pr[4, k3 - p1 - p2, 0] == 1/pr10, pr[4, k2 - p1, 0] == 1/pr11, 
 pr[4, k3 - p1, 0] == 1/pr12}

and want to export in in a file as

enter image description here

This can be printed very well as

Do[
  Print["id ", FirstPart[[kk, 1]], "=", FirstPart[[kk, 2]], ";"];
  , {kk, 1, Length[FirstPart]}];

But my question is can I save the print output in a txt file?

$\endgroup$
2
  • $\begingroup$ what is FirstPart ? is this the name of your list? $\endgroup$
    – Nasser
    Jan 5 at 8:51
  • $\begingroup$ yes sorry for not mentioning $\endgroup$ Jan 5 at 8:56

1 Answer 1

4
$\begingroup$

You could do

SetDirectory[NotebookDirectory[]]
lis = {pr[4, k1, 0] == 1/pr1, pr[4, k2, 0] == 1/pr2, 
   pr[4, k3, 0] == 1/pr3, pr[4, k1 - k2, mZ] == 1/pr4, 
   pr[4, k2 - k3, 0] == 1/pr5, pr[4, k1 - k3, 0] == 1/pr6, 
   pr[4, k1 - p1, 0] == 1/pr7, pr[4, k1 - p1 - p2, 0] == 1/pr8, 
   pr[4, k2 - p1 - p2, 0] == 1/pr9, pr[4, k3 - p1 - p2, 0] == 1/pr10, 
   pr[4, k2 - p1, 0] == 1/pr11, pr[4, k3 - p1, 0] == 1/pr12};

data = First@Last@Reap[Do[Sow[{"id ", lis[[kk, 1]], "=", lis[[kk, 2]], ";"}], 
           {kk, 1, Length[lis]}]]

Export["test.txt", data, "Table"]

And this gives this file

enter image description here

$\endgroup$

Your Answer

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

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