Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to import data from a CSV file into Mathematica that contains line feeds in certain elements that I wish to be properly rendered in Mathematica. I suppose this has something to do with the type of form (InputForm, StandardForm, etc.) that Mathematica uses on and after import. I know I am missing something fundamental but have not found it yet.

I do not want the solution to this problem to contain any Mathematica code in the CSV file.

Lets take graph labels as an example.

enter image description here

I would like one vertex label to have two on the first line and deux on the second line. I have tried entering the data in various ways with no success. Here is what I have tried.

  1. two[LineFeed]deux

Result: is shown above. There IS a linefeed but the control code name is also rendered.

  1. two\ndeux

Result: No line feed at all.

enter image description here

  1. two.0Adeux (i.e. using hexadecimal code)

Result: Same as [NewLine], get the line feed but the hex code is printed too.

Thanks in advance for your help.

Jeff Lapides

share|improve this question
    
Possibly a bug? ImportString[ExportString[{{1, "long\nthing"}, {2, "other"}}, "CSV", "TextDelimiters" -> "\""],"CSV", "TextDelimiters" -> "\""] returns {{"1,\"long"}, {"thing\""}, {2, "other"}} –  ssch Jun 14 '13 at 12:17
add comment

1 Answer

up vote 3 down vote accepted

I assume that your CSV has the following format, with vertices in column 1 and vertexlabels in column 2.

1   one\nun
2   two\ndeux
3   three\ntrois
4   four\nquatre
5   five\ncinq
6   six\nsix

I think the following might work:

labels = Import["label.csv"];
Graph[{1 -> 3, 1 -> 2, 2 -> 3, 2 -> 4, 4 -> 5, 5 -> 6, 6 -> 4}, 
 VertexLabels -> 
  Map[#[[1]] -> StringReplace[#[[2]], "\\n" -> "\n"] &, labels], 
 ImagePadding -> 25]

graph with labels

It looks like the Import command will escape the \n and you need to unescape it with a StringReplace.

share|improve this answer
    
This looks great. I will check it out tomorrow. Thank you very much. Jeff –  Jeff Lapides Jun 15 '13 at 1:20
    
Bob: I just checked this out and it works great. I tested on my test data set. Will check on the production one next. Thanks Again. –  Jeff Lapides Jun 16 '13 at 13:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.