1
$\begingroup$

I defined some variables as follows:

altura=15;comprimento=45;largura=92;

I have a TXT file that comes formatted from another application:

Import["C:\Users\Desktop\Text.txt"]

enter image description here

The other application exports some variables with quotation marks (").

I want to import this TXT file and swap the values ​​of these variables according to the values ​​I set previously and after changing the values ​​to be able to export them modified.

I would like to export the TXT file as follows:

enter image description here

Only the texts with numerical values that I want to change.

The next part of the term does not want changes:

enter image description here

I tried to use StringReplace, but I did not succeed.

Could anyone comment?

$\endgroup$
4
$\begingroup$

Here is your file:

file = "\"altura\"= 10\n\"comprimento\"= 30\nlargura= 50\n\"Altura@...\"= \"altura\"";

And here are your variables defined:

altura=15;comprimento=45;largura=92;

Here is a way to process the file:

table = ImportString[file, "Table", "FieldSeparators" -> {"= "}];
tableNew = Replace[
  table, {{"altura", _} -> {"altura", altura}, {"comprimento", _} -> {"comprimento", 
     comprimento}, {"largura", _} -> {"largura", largura}}, {1}]
{{"altura", 15}, {"comprimento", 45}, {"largura", 92}, {"Altura@...", "altura"}}

Here is a way to export the file backward manually:

ExportString[StringJoin@Riffle[Riffle[#, "= "] & /@
    (tableNew /. {s_String :> "\"" <> s <> "\"", n_Integer :> ToString[n]}), "\n"],
 "Text"]
"\"altura\"= 15
\"comprimento\"= 45
\"largura\"= 92
\"Altura@...\"= \"altura\""

And here is a better approach:

ExportString[tableNew, "Table", "FieldSeparators" -> "= ", "TextDelimiters" -> "\""]
"\"altura\"= 15
\"comprimento\"= 45
\"largura\"= 92
\"Altura@...\"= \"altura\""
$\endgroup$
  • $\begingroup$ I added the fileLocation variable to make it easier to replace the original file text $\endgroup$ – LCarvalho Jan 4 '18 at 14:49
  • $\begingroup$ Although its approximation shows an error with accented characters, I do not see problems in this. I just need to avoid them while creating the original file. Accentuations are not problems. $\endgroup$ – LCarvalho Jan 4 '18 at 14:53
  • $\begingroup$ If no other solution comes up so early, your answer is already solving my problem $\endgroup$ – LCarvalho Jan 4 '18 at 14:55
  • 2
    $\begingroup$ @LCarvalho You shouldn't replace the actual file contents with fileLocation because it makes the outputs shown in my answer non-reproducible. Actually I did should ask you before answering for providing explicit copyable file contents instead of a screenshot, as it is accepted here. Providing screenshots isn't an acceptable practice here, and a copyable example must be always given. So I revert your change and ask you to replace the screenshots with copyable inputs. $\endgroup$ – Alexey Popkov Jan 4 '18 at 14:58
  • $\begingroup$ Thanks for informing! I'll be more careful about this kind of situation. $\endgroup$ – LCarvalho Jan 4 '18 at 15:19

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.