Import and export for CSV violated in Mathematica

Consider the following 2: 2 array:

x = {{"abc", "1,2,3"}, {"i \"comma-heart\" you", "i \",heart\" u, too"}} 

If we export this to CSV and then import it again, we will not get the same:

 Import[Export["tmp.csv", d]] 

Looking at tmp.csv, it is clear that Export did not work because the quotation marks are not escaped properly.

According to the RFC , which I believe is correctly summarized on Wikipedia Entries in CSV , the correct way to export the above array is as follows:

 abc, "1,2,3" "i ""heart"" you", "i "",heart"" u, too" 

Importing the above does not give the original array. So the import is also broken.

I reported these errors on support@wolfram.com , but I wonder if others have any workarounds.

One way to solve the problem is to use TSV instead of CSV. I tested above with TSV and it seems to work (even with tabs embedded in array entries).

+4
source share
1 answer

Instead of TSV, another workaround is to use a different delimiter:

 In[26]:= str = ExportString[x, "CSV", "TextDelimiters"->"'"]; Out[26]= "'ab c','1,2,3' 'i \"comma-heart\" you','i \",heart\" u, too'" In[27]:= y = ImportString[str, "CSV", "TextDelimiters"->"'"] Out[27]= {{"abc", "1,2,3"}, {"i \"comma-heart\" you", "i \",heart\" u, too"}} In[28]:= x == y Out[28]= True 

Please note that Import / Export and ImportString / ExportString use the same parameters, the latter only reads / writes lines instead of files.

You can also use one of the other table / scientific data formats supported by Mathematica, such as XLS, ODS, HDF, HDF5, CDF, FITS, etc.

You can also find some of them faster, since some of them are binary and therefore there is no text analysis. It all depends on your application and the fact that this file is used outside of Mathematica.

+2
source

Source: https://habr.com/ru/post/1308823/


All Articles