0
$\begingroup$

I'm having trouble importing a CSV file.

The CSV file doesn't have simple dimensions, some rows may have many more columns than others.

From my imported file I cannot access the importeddata part specification below the second level.

Is there a better format to use?

info = {{{"Start", {4, 4, 5}, {0, 0, -1}, 
     1715}, {"End", {4, 4, -95}, {0, 0, -1}, 
     1715}}, {{"Start", {4, 4, 5}, {0, 0, -1}, 
     1715}, {"Process 2", {1, 4, 5}, {0, 0, -1}, 
     1715}, {"End", {1, 4, 5}, {0, 0, -1}, 
     1715}}, {{"Start", {4, 4, 5}, {0, 0, -1}, 
     1715}, {"Process 5", {4, 6, -95}, {0, 0, -1}, 
     115}, {"End", {3, 4, -9}, {0, 0, -1}, 1005}}};


Export["info.csv", info];
importdata = Import["info.csv", "CSV"];

(*this works *) 
info[[1]][[1]][[2]][[2]]
(*this doesn't*) 
importdata[[1]][[1]][[2]]
(*but this does *) 
importdata[[1]][[1]]
$\endgroup$
  • $\begingroup$ importdata = Map[ToExpression] @ Import["info.csv","CSV"] $\endgroup$ – Jason B. Jan 23 '18 at 18:40
  • 2
    $\begingroup$ Your data isn't a simple 2D array, so the exporter just made every line a string I believe. If you have a choice of output formats, for this type of data JSON is perfect. Use Export["info.json", info, "RawJSON"] and importdata = Import["info.json", "RawJSON"] and the data round trips perfectly. $\endgroup$ – Jason B. Jan 23 '18 at 18:42
  • 1
    $\begingroup$ if you don't need to share the data with other programs, another option is to use Put/Get: info >> "file.m" importdata=Get["file.m"] will guarantee exactly the same in as out. $\endgroup$ – george2079 Jan 23 '18 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.