0
$\begingroup$

I have a text file which I have constructed as

savedXText = ExportString[savedX, "Text"]
savedXText2 = 
  StringReplace[
   savedXText, {"[" -> "(", "]" -> ")", 
    "t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}];
Export["testStack.txt", savedXText2];

This will be used later on in a python program.

testStack.txt has three rows (however, in the future it could have more or less depending on the number of nodes I am working with). Unfortunately, the code doesn't do what I need.

I would like to know if there is a way to aknowledge the row in which one is by writing it in lammda and phi. i.e for the first row I would like to know if

"t" -> "m.lammda1[i]*180/np.pi" and "u" -> "m.phi1[i]*180/np.pi

can be written and in a similar fashion,

"t" -> "m.lammda2[i]*180/np.pi" and "u" -> "m.phi2[i]*180/np.pi for the second row and so on.

I am certain that it can be done by using a for loop or a table where a variable takes the value of the row of savedText (the original before the StringReplace) and then use StringForm or toString in the StringReplace, but I cannot do it.

This is the first row of testStack.txt

-19.995904426195736*Sinc(0.04188790204786391*(0. + m.lammda[i]*180/np.pi))*Sinc(0.08975979010256552*(-65. + m.phi1[i]*180/np.pi)) - 37.58848602557134*Sinc(0.04188790204786391*(75. + m.lammda[i]*180/np.pi))*Sinc(0.08975979010256552*(-65. + m.phi1[i]*180/np.pi)) - 26.820905985723094*Sinc(0.04188790204786391*(0. + m.lammda[i]*180/np.pi))*Sinc(0.08975979010256552*(-30. + m.phi1[i]*180/np.pi)) - 15.904862946583352*Sinc(0.04188790204786391*(75. + m.lammda[i]*180/np.pi))*Sinc(0.08975979010256552*(-30. + m.phi1[i]*180/np.pi))
$\endgroup$
  • $\begingroup$ Have you looked at MapIndexed? If the rows are split into a list, then MapIndexed may be used to get the row number (the index) and perhaps this will do what you want. $\endgroup$ – Mark R Feb 4 at 21:18
1
$\begingroup$

Something like this may do it:

editLine = "a * Sinc[ b*t + c*u ]";
noEdit = "# COMMENT";

text = {noEdit, noEdit, editLine, editLine, noEdit, editLine};

count = 0;
newText = StringReplace[text, {
    "[" -> "(", "]" -> ")",
    "t" :> ("m.lammda" <> ToString[++count] <> "[i]*180/np.pi"),
    "u" :> ("m.phi" <> ToString[count] <> "[i]*180/np.pi")}];

newText // ColumnForm


(* 

# COMMENT
# COMMENT
a * Sinc( b*m.lammda1[i]*180/np.pi + c*m.phi1[i]*180/np.pi )
a * Sinc( b*m.lammda2[i]*180/np.pi + c*m.phi2[i]*180/np.pi )
# COMMENT
a * Sinc( b*m.lammda3[i]*180/np.pi + c*m.phi3[i]*180/np.pi )

*)
| improve this answer | |
$\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.