Message Boards Message Boards

GROUPS:

Import csv with the semicolon seperator

Posted 3 months ago
671 Views
|
4 Replies
|
2 Total Likes
|

I have the following csv file (attached file)

ch1;ch2;ch3
10;1;100
20;2;200
30;3;300
40;4;400
50;5;500

I want to import it to Mathematica as a Dataset, and I tried the following command:

Import["ref.csv", "Dataset", "FieldSeparators" -> {";"}]

which gave me the following:

enter image description here

whereas what I want is obviously

enter image description here

Any help?

Attachments:
4 Replies
Posted 3 months ago

Hi Ehud,

The attached "ref.csv" is comma separated, not ;. Anyway "FieldSeparators" is not an option for CSV import. Workaround

Import["path/to/file", "Table", "FieldSeparators" -> {";"}] // Dataset
Posted 3 months ago

I opened the file in a text editor and found that your file has commas, not semi colons.

Posted 3 months ago

You are right. Sorry.

I updated the csv file attached.

A string split on each line would also do the trick:

Dataset[Flatten[StringSplit[#, ";"]] & /@ Import["path/to/file"]]

If it's a larger dataset, then I would suggest using BinaryReadList to make it import more quickly:

Dataset[StringSplit[#, ";"] & /@ 
  StringSplit[
   FromCharacterCode[BinaryReadList["path/to/file"], "UTF-8"], 
   "\r\n"]]
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract