Go to file
Yasuhiro Hatta 117640b9e9 Bump version 1.1.2 2016-11-29 21:43:46 +09:00
CSV.xcodeproj Update project.pbxproj 2016-11-14 00:42:43 +09:00
Sources Bump version 1.1.2 2016-11-29 21:43:46 +09:00
Tests Hook up unit tests for Linux-based builds 2016-11-28 22:18:40 +01:00
.gitignore Initial commit 2016-06-11 02:10:59 +09:00
.swift-version Add .swift-version 2016-09-25 02:06:44 +09:00
.travis.yml Update .travis.yml 2016-08-21 18:52:04 +09:00
CSV.swift.podspec Bump version 1.1.2 2016-11-29 21:43:46 +09:00
LICENSE Initial commit 2016-06-11 02:10:59 +09:00
Package.swift Add Package.swift 2016-06-11 12:08:43 +09:00
README.md Update README.md 2016-10-29 11:27:08 +09:00

README.md

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.