Question: how to prevent ExportMatrix from adding an extra \ when exporting as CSV?

I have plain text file generated, where each line has mixed numbers and strings. Some of the strings are latex. So they have "\" in them. The data is rectangle. same number of fields on each line, separated by commas.  I use CSV format to read the data to Maple.

I simply want to read this file using Import , which reads it into a matrix (using CSV format), and export it back right away using either ExportMatrix or Export and end up with the same exact file.

But Maple always adds extra "\" each time I export the matrix back to the file. So if I have initially "\sin x" in the file, after reading/writing the file 5 times without touching the data inside Maple, I end up with something like "\\\\\\\\\\\sin x" in the file at the end.

I could ofcourse not use ExportMatrix nor Export and write the data back to the file manually using fprintf using correct format for each field. But it is easier if I can get ExportMatrix to work since there are many fields some are integers, some are real and some are strings.

Here is a simple MWE which shows the problem. Originally I have this file on disk. Called test.txt and has 2 lines in it

1,"\sin x"
2,"this is second line"

Now after doing the following

data:=Import("test.txt",format="CSV",output=Matrix):  
ExportMatrix("test.txt",data,target=csv);

The file on disk now becomes

1,"\\sin x"
2,"this is second line"

And repeating the above one more time, the file becomes

1,"\\\\sin x"
2,"this is second line"

This ofcourse breaks all the latex strings in there.

Is there a way to prevent Maple from doing this? I looked at all the options in help, but do not see on so far.  I also tried Export but that did not work at all. So I'd like to see if ExportMatrix can do it, without adding an extra "\", otherwise, will have to use fprintf directly to export the data back to file.

edit

fyi, I ended up using fprintf to write the data back to disk. May be if there is a solution using ExportMatrix, will change back. This is what I ended up doing

File before

1,"\sin x"
2,"this is second line"

Now run this code

data:=Import("test.txt",format="CSV",output=Matrix):  
file_id := fopen("test.txt",WRITE):
nRows:=LinearAlgebra:-RowDimension(data);
for n to nRows do
    if n<nRows then
       fprintf(file_id,"%d,\"%s\"\n",data[n,1],data[n,2]);
    else
       fprintf(file_id,"%d,\"%s\"",data[n,1],data[n,2]);
    fi;
od;
fclose(file_id);

File after

1,"\sin x"
2,"this is second line"

Now the file is exactly the same as before reading it.

My actual data is more than the above two fields, but that makes the format string longer, that is all.

 

Please Wait...