The "PrintableASCII"
characters set consists of 97 characters, including space (\[RawSpace]
) and two control characters: the newline character \n
(\[NewLine]
) and the carriage return \r
(\[RawReturn]
), which are the only allowed WhitespaceCharacter
s in this encoding (highlighted by light blue in the following table):
printableASCIISet =
Cases[ToCharacterCode[FromCharacterCode[Range[0, 255]], "PrintableASCII"], k_Integer];
printableASCIISet // Length
Grid[
Flatten[
Transpose /@
Partition[{#, FromCharacterCode[#]} & /@ printableASCIISet, UpTo@20], 1],
Frame -> {All, {{True, False}}}, Spacings -> 0.2, FrameStyle -> LightGray] /.
s_String /; StringMatchQ[s, WhitespaceCharacter] :>
Item[StringTrim[ToString[InputForm@s], "\""], Background -> LightBlue]
97
The "ASCII"
encoding allows all control characters and consists of the characters with codes from 0 to 127.
I wish to allow only newlines (\n
), carriage returns (\r
), spaces () and tabs (
\t
) as whitespace characters when exporting as ASCII. So the only difference with "PrintableASCII"
is that tabs are also allowed, and of course all the special, extended-ASCII and Unicode characters must be converted into their corresponding FullForm
s.
Here are example strings with expected output in the exported textual file:
Input:
ExportAsASCII["test1.txt", "Here is a string in quotes:\n\"quoted string\""]
Verbatim contents of the created file "test1.txt":
Here is a string in quotes: "quoted string"
(note the absence of the outer quotation marks and also that the inner quotes aren't escaped).
Input:
ExportAsASCII["test2.txt", "Lamé \[LongRightArrow] αβ+"]
Verbatim contents of the created file "test2.txt":
Lam\[EAcute] \[LongRightArrow] \[Alpha]\[Beta]+
(note the
é
encoded as\[EAcute]
, not as\\[EAcute]
).Input:
s = "\!\(\r\n\*SubsuperscriptBox[\(\[Integral]\), \(-Pi\), \(Pi\)]\(g[\r\n\*SuperscriptBox[\(x\), \(2\)] + \r\n\*SuperscriptBox[\(x\), \(3\)] + \r\n\*SuperscriptBox[\(x\), \(6\)]] \[DifferentialD]x\)\)" ExportAsASCII["test3.txt", s]
Verbatim contents of the created file "test3.txt":
\!\( \*SubsuperscriptBox[\(\[Integral]\), \(-Pi\), \(Pi\)]\(g[ \*SuperscriptBox[\(x\), \(2\)] + \*SuperscriptBox[\(x\), \(3\)] + \*SuperscriptBox[\(x\), \(6\)]] \[DifferentialD]x\)\)
(note that the newline characters aren't escaped and present here as actual new lines).
How is it possible to implement ExportAsASCII
?
P.S. Actually the above-described behavior is (by the essence) what I expected for Export
ing as "String"
with CharacterEncoding -> "ASCII"
according to the Documentation:
Export["test.txt", "Lamé \[LongRightArrow] αβ+", "String", CharacterEncoding -> "ASCII"]
But currently "ASCII"
as well as "PrintableASCII"
and None
seem to be simply ignored by Export
. :(
fullname[c_String]:=StringReplace[ToString[c,InputForm,CharacterEncoding->"ASCII"],"\"\\["~~a__~~"]\"":>"\\["~~a~~"]"]
. $\endgroup$\[CirclePlus]
. Should it be counted as a bug? $\endgroup$