18
$\begingroup$

When importing a csv file is there a way to set the delimiter. I have a csv that uses ";" as its delimiter.

I tried

 rd = import["/Users/rolfbosscha/Downloads/rsdata.csv"]
$\endgroup$
2
  • 1
    $\begingroup$ You can use ReadList which happens to be much faster than Import See for example mathematica.stackexchange.com/questions/35371/…. With only a slight modification: change "," to ";" under RecordSeperators $\endgroup$
    – RunnyKine
    Nov 19, 2013 at 21:14
  • 3
    $\begingroup$ Mathematica is case-sensitive: use Import, not import. $\endgroup$
    – m_goldberg
    Nov 19, 2013 at 21:21

2 Answers 2

40
$\begingroup$

This seems to work:

Import["data.csv", "table", FieldSeparators -> ";"]

Should be

Import["data.csv", "Table", "FieldSeparators" -> ";"]

See help for "Table" element (ref/format/Table), specifically Options section.

$\endgroup$
7
  • 2
    $\begingroup$ This does not work with tabs \t in Mathematica 11.0.1, Debian 8.5. $\endgroup$ Oct 28, 2016 at 14:33
  • $\begingroup$ it works. thanks $\endgroup$
    – Nam Nguyen
    Jan 13, 2017 at 15:15
  • $\begingroup$ Yes, what todo with tabs? \t does not work. $\endgroup$
    – a06e
    Sep 22, 2017 at 10:14
  • $\begingroup$ If you have \t then your file format is "Table". Simply: Import["data.csv", "Table"] $\endgroup$
    – drgrujic
    Feb 1, 2018 at 15:08
  • $\begingroup$ @drgrujic wrong, with tabs, it's best to use TSV. $\endgroup$ Dec 11, 2019 at 15:49
5
$\begingroup$

I may be mistaken here, but I don't think you can switch from the standard CSV definition (commas separate values) to the one you have (commas take the role of decimal points, and semicolons are used as value separator). A workaround might be to first import as text, switch the offending characters and import that.

As a demo I use ImportString here instead of Import, but it should work almost the same for Import too.

ImportString[
  StringReplace[ 
     ImportString["1,2;2;3;4.3\n2,3;4,5;6;7", "Text"], 
     {"," -> ".", ";" -> ","}
  ], 
  "CSV"
]

{{1.2, 2, 3, 4.3}, {2.3, 4.5, 6, 7}}

Replace the inner ImportString with

Import["/Users/rolfbosscha/Downloads/rsdata.csv", "Text"]

to work with your file. Note that you need to use "Text" instead of "CSV".

$\endgroup$
2
  • $\begingroup$ What do you think about Jukka's answer? I cannot make it to work. I think your approach can work. My deliminator is \t. $\endgroup$ Oct 28, 2016 at 14:34
  • 2
    $\begingroup$ If your separator is a tab, import as TSV. $\endgroup$ Oct 28, 2016 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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