Skip to content

CSV reading library written in Swift.

License

Notifications You must be signed in to change notification settings

bencallis/CSV.swift

 
 

Repository files navigation

CSV.swift

Build Status

CSV reading library written in Swift.

Usage

From CSV string

import CSV

for row in try! CSV(string: "1,foo\n2,bar") {
    print("\(row)")
    // => ["1", "foo"]
    // => ["2", "bar"]
}

From file path

import Foundation
import CSV

let stream = InputStream(fileAtPath: "/path/to/file.csv")!
for row in try! CSV(stream: stream) {
    print("\(row)")
}

Getting the header 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"]
}

Get the field value using subscript

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"
}

Provide the character encoding

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)

Installation

CocoaPods

pod 'CSV.swift', '~> 1.1'

Carthage

github "yaslab/CSV.swift" ~> 1.1

Swift Package Manager

import PackageDescription

let package = Package(
    name: "PackageName",
    dependencies: [
        .Package(url: "https://github.com/yaslab/CSV.swift.git", majorVersion: 1, minor: 1)
    ]
)

Reference specification

License

CSV.swift is released under the MIT license. See the LICENSE file for more info.

About

CSV reading library written in Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.1%
  • Ruby 1.3%
  • Objective-C 0.6%