# Changing Text with Previous Quotation Marks

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"]


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:

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

The next part of the term does not want changes:

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

Could anyone comment?

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\""

• I added the fileLocation variable to make it easier to replace the original file text – LCarvalho Jan 4 '18 at 14:49
• 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. – LCarvalho Jan 4 '18 at 14:53
• If no other solution comes up so early, your answer is already solving my problem – LCarvalho Jan 4 '18 at 14:55
• @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. – Alexey Popkov Jan 4 '18 at 14:58
• Thanks for informing! I'll be more careful about this kind of situation. – LCarvalho Jan 4 '18 at 15:19