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

  • $\begingroup$ Possibly a bug? ImportString[ExportString[{{1, "long\nthing"}, {2, "other"}}, "CSV", "TextDelimiters" -> "\""],"CSV", "TextDelimiters" -> "\""] returns {{"1,\"long"}, {"thing\""}, {2, "other"}} $\endgroup$
    – ssch
    Jun 14, 2013 at 12:17

1 Answer 1


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.

  • $\begingroup$ This looks great. I will check it out tomorrow. Thank you very much. Jeff $\endgroup$ Jun 15, 2013 at 1:20
  • $\begingroup$ 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. $\endgroup$ Jun 16, 2013 at 13:22

Your Answer

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

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