1
$\begingroup$

I have a problem with importing and extracting 2nd column in my CSV file. I need to have 3 list, the first one first column except for first row, the second will be second column except for 1st row and the last one will be 3rd column without first row. I have succesfully imported and extracted the first column using:

Centre = Import["C:\\Users\\vocko01\\Desktop\\TS.csv", "Table", 
   "FieldSeparators" -> ";"][[2 ;;, 1]]

The third one works too:

Suburb = Import["C:\\Users\\vocko01\\Desktop\\TS.csv", "Table", 
   "FieldSeparators" -> ";"][[2 ;;, -1]]

But when I want to import and extract 2nd column, it doesn´t work the same way:

City = Import["C:\\Users\\vocko01\\Desktop\\TS.csv", "Table", 
   "FieldSeparators" -> ";"][[2 ;;, 2]]

Can you please help me with this issue or is it a better way to write it?

Thank you

Here is the link for CSV file: https://gofile.io/d/ELwAhG

$\endgroup$
4
  • 1
    $\begingroup$ As a general practice, I don't think it's a good idea to name a file .csv when it doesn't have comma separators. $\endgroup$ Commented Oct 22, 2020 at 10:32
  • 1
    $\begingroup$ @SjoerdSmit: in principle I agree with your comment, but I know that it is common standard on at least German computers that .csv files use ";" as a separator because the German standard for NumberPoint is ",". For example if you save as csv from Excel on a German computer, it will use ";" as separator and most other spreadsheet apps do that as well. $\endgroup$ Commented Oct 22, 2020 at 16:45
  • $\begingroup$ @AlbertRetey Because of course... Yeah, I should've know that one. Dutch also uses commas as decimal separators, I just never used CSV on a Dutch computer (since I always set everything to English.). I just thought that something a ubiquitous as CSV would have a common international standard. Of course that would be too much to ask for... $\endgroup$ Commented Oct 22, 2020 at 18:07
  • 3
    $\begingroup$ @SjoerdSmit I had the exact same reaction to these comments, except I only speak English so I would have never even thought of it :) I should add support for ; in the native CSV importer because of this. $\endgroup$ Commented Oct 22, 2020 at 19:49

2 Answers 2

2
$\begingroup$

Your data contains missing entries like: {{";0;0"}, {";0;87"}, {";0;"}}. You must first repair the missing elements. Towards this aim, you first read the data as a string. Then you use StringReplace to add the missing pieces. Finally you import the data using StringImport

dat = Import["yourFileName", "String"];
dat = StringReplace[dat, {"\n;" -> "\n0;", ";\r" -> ";0\r"}];;
dat = ImportString[dat, "Table", "FieldSeparators" -> ";"];

Now you can extract the columns as before.

$\endgroup$
1
$\begingroup$

I recommend importing the file like this to make it rectangular:

table = PadRight[
   Import["TS.csv", "Table", "FieldSeparators" -> ";"],
   Automatic,
   Missing[]
];
Dimensions[table]

{17349, 3}

Create a nice dataset:

dataset = Dataset[AssociationThread[First[table], #]& /@ Rest[table]]
$\endgroup$

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.