CSV reading library written in Swift.
import CSV
for row in try! CSV(string: "1,foo\n2,bar") {
print("\(row)")
// => ["1", "foo"]
// => ["2", "bar"]
}
import Foundation
import CSV
let stream = InputStream(fileAtPath: "/path/to/file.csv")!
for row in try! CSV(stream: stream) {
print("\(row)")
}
import CSV
let csv = try! CSV(
string: "id,name\n1,foo\n2,bar",
hasHeaderRow: true) // default: false
let headerRow = csv.headerRow!
print("\(headerRow)") // => ["id", "name"]
for row in csv {
print("\(row)")
// => ["1", "foo"]
// => ["2", "bar"]
}
import CSV
var csv = try! CSV(
string: "id,name\n1,foo",
hasHeaderRow: true) // It must be true.
while let _ = csv.next() {
print("\(csv["id"]!)") // => "1"
print("\(csv["name"]!)") // => "foo"
}
If you use a file path, you can provide the character encoding to initializer.
import Foundation
import CSV
let csv = try! CSV(
stream: InputStream(fileAtPath: "/path/to/file.csv")!,
codecType: UTF16.self,
endian: .big)
pod 'CSV.swift', '~> 1.1'
github "yaslab/CSV.swift" ~> 1.1
import PackageDescription
let package = Package(
name: "PackageName",
dependencies: [
.Package(url: "https://github.com/yaslab/CSV.swift.git", majorVersion: 1, minor: 1)
]
)
CSV.swift is released under the MIT license. See the LICENSE file for more info.