Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 720 characters in body
Source Link
Fred Simons
  • 10.2k
  • 19
  • 50

The following is a minimal working example of a problem that I met when I was programmatically constructing codefiles. I use Mathematica under Windows.

I have 5 strings, all containing an arrow character that is entered in the usual way esc - > esc. Two of the strings contain a quotation mark as well.

teststrings = {"a→b", "a\"c→b", "a→b", "a\"c→b", "a→b" }

(* {a→b,a"c→b,a→b,a"c→b,a→b} *)

When printed as output in the notebook, all arrows turn up as a single character. Now I write these strings to a file:

file = OpenWrite[FileNameJoin[{NotebookDirectory[], "test.m"}]];
Do[ WriteLine[file, x], {x, teststrings}];
Close[file];

When I inspect the file, it looks like so:

a→b
a"c->b
a->b
a"c→b
a→b

After the first quotation mark has been written to the file, the following arrows are written incorrectly as two characters, until the second quotation mark has been written to the file. After that, the arrows appear to be correct again.

I fail to see an explanation for this behaviour and I fail to see a workaround. My setting of $CharacterEncoding is WindowsANSI. Is there a better way of doing this?

Edit

When I open the file in a text editor such as Notepad++, I indeed see 5 double arrows. So now I realize that I should have been more explicit. As I wrote, I want to have a Mathematica code file, and I inspected the file with Mathematica itself. Thanks for all comments mentioning that the file itself does not contain any single character arrow, only -> for an arrow.

So the problem is not the writing of the file, but the reading of the .m-file by Mathematica. It seems that when Mathematica reads a .m-file, the two characters -> are replaced with → until an escaped quotation mark is read, and then does not replace until the next reading of an escaped quotation mark. Strange. Is this as intended?

The following is a minimal working example of a problem that I met when I was programmatically constructing codefiles. I use Mathematica under Windows.

I have 5 strings, all containing an arrow character that is entered in the usual way esc - > esc. Two of the strings contain a quotation mark as well.

teststrings = {"a→b", "a\"c→b", "a→b", "a\"c→b", "a→b" }

(* {a→b,a"c→b,a→b,a"c→b,a→b} *)

When printed as output in the notebook, all arrows turn up as a single character. Now I write these strings to a file:

file = OpenWrite[FileNameJoin[{NotebookDirectory[], "test.m"}]];
Do[ WriteLine[file, x], {x, teststrings}];
Close[file];

When I inspect the file, it looks like so:

a→b
a"c->b
a->b
a"c→b
a→b

After the first quotation mark has been written to the file, the following arrows are written incorrectly as two characters, until the second quotation mark has been written to the file. After that, the arrows appear to be correct again.

I fail to see an explanation for this behaviour and I fail to see a workaround. My setting of $CharacterEncoding is WindowsANSI. Is there a better way of doing this?

The following is a minimal working example of a problem that I met when I was programmatically constructing codefiles. I use Mathematica under Windows.

I have 5 strings, all containing an arrow character that is entered in the usual way esc - > esc. Two of the strings contain a quotation mark as well.

teststrings = {"a→b", "a\"c→b", "a→b", "a\"c→b", "a→b" }

(* {a→b,a"c→b,a→b,a"c→b,a→b} *)

When printed as output in the notebook, all arrows turn up as a single character. Now I write these strings to a file:

file = OpenWrite[FileNameJoin[{NotebookDirectory[], "test.m"}]];
Do[ WriteLine[file, x], {x, teststrings}];
Close[file];

When I inspect the file, it looks like so:

a→b
a"c->b
a->b
a"c→b
a→b

After the first quotation mark has been written to the file, the following arrows are written incorrectly as two characters, until the second quotation mark has been written to the file. After that, the arrows appear to be correct again.

I fail to see an explanation for this behaviour and I fail to see a workaround. My setting of $CharacterEncoding is WindowsANSI. Is there a better way of doing this?

Edit

When I open the file in a text editor such as Notepad++, I indeed see 5 double arrows. So now I realize that I should have been more explicit. As I wrote, I want to have a Mathematica code file, and I inspected the file with Mathematica itself. Thanks for all comments mentioning that the file itself does not contain any single character arrow, only -> for an arrow.

So the problem is not the writing of the file, but the reading of the .m-file by Mathematica. It seems that when Mathematica reads a .m-file, the two characters -> are replaced with → until an escaped quotation mark is read, and then does not replace until the next reading of an escaped quotation mark. Strange. Is this as intended?

Source Link
Fred Simons
  • 10.2k
  • 19
  • 50

Writing strings with an arrow and escaped quotation marks

The following is a minimal working example of a problem that I met when I was programmatically constructing codefiles. I use Mathematica under Windows.

I have 5 strings, all containing an arrow character that is entered in the usual way esc - > esc. Two of the strings contain a quotation mark as well.

teststrings = {"a→b", "a\"c→b", "a→b", "a\"c→b", "a→b" }

(* {a→b,a"c→b,a→b,a"c→b,a→b} *)

When printed as output in the notebook, all arrows turn up as a single character. Now I write these strings to a file:

file = OpenWrite[FileNameJoin[{NotebookDirectory[], "test.m"}]];
Do[ WriteLine[file, x], {x, teststrings}];
Close[file];

When I inspect the file, it looks like so:

a→b
a"c->b
a->b
a"c→b
a→b

After the first quotation mark has been written to the file, the following arrows are written incorrectly as two characters, until the second quotation mark has been written to the file. After that, the arrows appear to be correct again.

I fail to see an explanation for this behaviour and I fail to see a workaround. My setting of $CharacterEncoding is WindowsANSI. Is there a better way of doing this?