I have a mathematica program which output is a text file that for reasons will be used in python as input.
savedXText = ExportString[savedX, "Text"];
savedXText2 =
StringReplace[
savedXText, {"[" -> "(", "]" -> ")",
"t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}];
Export["testOutputX.txt", savedXText2];
testOutputX.txt
has a number of rows which can vary. I need to write instead of just m.lammda
and m.phi
the number of row. If the file has three rows, I would need m.lammda1
and m.phi1
, m.lammda2
and m.phi2
and m.lammda3
and m.phi3
in each row of testOutputX.txt
My idea is to use a for loop, charge each row of savedX
and then do the string replace in which the value of the variable used to count the loop is written as text in the StringReplace
.
I have constructed a for loop that picks each row of savedX
and does the StringReplace
savedXReplaced = Table[RandomInteger[i, lengthLoop], {i, 1, lengthLoop}];
For[i = 1, i < lengthLoop + 1, i++,
savedXTextTemp = ExportString[savedX[[i]], "Text"];
savedXTextTemp2 =
StringReplace[
savedXTextTemp, {"[" -> "(", "]" -> ")",
"t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}];
Print[savedXTextTemp2]; savedXReplaced[[i]] = savedXTextTemp2]
I however have looked at the documentation of StringReplace
and I haven't been able to find how to write the value of i
, the variable used in this for loop.
Is there a way to do this?
This should do the trick.
For[i = 1, i < lengthLoop + 1, i++,
savedXTextTemp = ExportString[savedX[[i]], "Text"];
savedXTextTemp2 = StringReplace[
savedXTextTemp, {"[" -> "(", "]" -> ")",
"t" -> StringJoin["m.lammda", ToString[i], "*180/np.pi"],
"u" -> StringJoin["m.phi", ToString[i], "*180/np.pi"]}];
Print[savedXTextTemp2]; savedXReplaced[[i]] = savedXTextTemp2]