3
$\begingroup$

I have a file, say temp.m, with a single line (* foo\[AAcute]bar *). What function call should I use to read the content of the file into the expression "foo\[AAcute]bar" inside Mathematica?

BTW, by "escaped non-ASCII characters" I mean objects like \[AAcute]. What's accurate term to call them in Wolfram Language terminology?

$\endgroup$

1 Answer 1

1
$\begingroup$

I think this might be considered a duplicate of Showing special characters in text containing unicode even though that question does not directly relate to importing. Applying Sjoerd's method:

Import["temp.m", "String"]

ToExpression["\"" <> % <> "\""]
"(* fooábar *)"

If you want only the content of comments then:

Import["temp.m", "Comments"]
ToExpression["\"" <> % <> "\""]
"fooábar"
$\endgroup$
2
  • $\begingroup$ For mission-critical purposes such as launching nuclear missiles, is the ToExpression safe? $\endgroup$
    – user13253
    Jan 4, 2017 at 18:59
  • $\begingroup$ @qazwsx Let me think about that. At least a minor additional layer of security might be had with the use of ToHeldExpression in place of ToExpression. $\endgroup$
    – Mr.Wizard
    Jan 5, 2017 at 2:54

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.