FlineDev / CSVImporter

Import CSV files line by line with ease

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading the CSV while considering the columns and the headers?

Halimeh-manaf opened this issue · comments

I have a CSV file which is structured like this:
Criteria First Name Last Name
Age X Y

Is there anyway to read the file while considering the column header?

What I want is something similar to record["Age"]["FirstName"] and it prints X

Thanks.

See the example in the section Header Structure Support in the README. Basically record["Age"] and record["FirstName"] are possible, but record["Age"]["FirstName"] isn't possible due to the row-based structure definition of CSV. We could add support for the requested feature though, which would be something like headerColumnsFromLeft or similar, but I'm not sure if that's a feature much needed so we should implement this in the framework itself.

What you could easily do is to create a dictionary and place your rows in there using the Age column, something like this:

let path = "path/to/Hogwarts/students"
let importer = CSVImporter<[String: String]>(path: path)
var studentsByAge: [String: [String: String]] = [:]
importer.startImportingRecords { $0 }.onFinish { importedRecords in
    for record in importedRecords {
        studentsByAge[record["Age"]] = record
    }
    // use your dictionary here ...
}

But I'm not sure if that's actually what you were looking for ...

See the example in the section Header Structure Support in the README. Basically record["Age"] and record["FirstName"] are possible, but record["Age"]["FirstName"] isn't possible due to the row-based structure definition of CSV. We could add support for the requested feature though, which would be something like headerColumnsFromLeft or similar, but I'm not sure if that's a feature much needed so we should implement this in the framework itself.

What you could easily do is to create a dictionary and place your rows in there using the Age column, something like this:

let path = "path/to/Hogwarts/students"
let importer = CSVImporter<[String: String]>(path: path)
var studentsByAge: [String: [String: String]] = [:]
importer.startImportingRecords { $0 }.onFinish { importedRecords in
    for record in importedRecords {
        studentsByAge[record["Age"]] = record
    }
    // use your dictionary here ...
}

But I'm not sure if that's actually what you were looking for ...

Thanks a lot. It cleared things up.

Regards.