Message Boards Message Boards

0
|
4828 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Import csv with the semicolon seperator

Posted 3 years ago

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:
POSTED BY: Ehud Behar
4 Replies
Posted 3 years 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 BY: Rohit Namjoshi

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"]]
POSTED BY: Steve Flanagan

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

POSTED BY: Gustavo Delfino
Posted 3 years ago

You are right. Sorry.

I updated the csv file attached.

POSTED BY: Ehud Behar
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