Need help with CSV.swift?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

yaslab
530 Stars 54 Forks MIT License 264 Commits 21 Opened issues

Description

CSV reading and writing library written in Swift.

Services available

!
?

Need anything else?

Contributors list

# 151,372
Swift
Shell
touch-i...
gcd
184 commits
# 94,050
Objecti...
Swift
delegat...
kvo
5 commits
# 63,408
Objecti...
swift-s...
lambda
alexa-s...
3 commits
# 663,939
Swift
Objecti...
CSV
1 commit
# 563,390
Swift
Objecti...
model-c...
formal-...
1 commit
# 3,218
Ruby
Swift
macOS
tint
1 commit
# 653,490
Swift
Objecti...
CSV
1 commit
# 660,324
Swift
Objecti...
CSV
1 commit
# 25,959
cpluspl...
php7
reveal-...
real-es...
1 commit
# 172,442
Swift
Shell
core-da...
swift5
1 commit

CSV.swift

Build Status codecov Open Source Helpers

CSV reading and writing library written in Swift.

Usage for reading CSV

From string

import CSV

let csvString = "1,foo\n2,bar" let csv = try! CSVReader(string: csvString) while let row = csv.next() { print("(row)") } // => ["1", "foo"] // => ["2", "bar"]

From file

NOTE: The default character encoding is

UTF8
.
import Foundation
import CSV

let stream = InputStream(fileAtPath: "/path/to/file.csv")! let csv = try! CSVReader(stream: stream) while let row = csv.next() { print("(row)") }

Getting the header row

import CSV

let csvString = "id,name\n1,foo\n2,bar" let csv = try! CSVReader(string: csvString, hasHeaderRow: true) // It must be true.

let headerRow = csv.headerRow! print("(headerRow)") // => ["id", "name"]

while let row = csv.next() { print("(row)") } // => ["1", "foo"] // => ["2", "bar"]

Get the field value using subscript

import CSV

let csvString = "id,name\n1,foo" let csv = try! CSVReader(string: csvString, hasHeaderRow: true) // It must be true.

while csv.next() != nil { 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 stream = InputStream(fileAtPath: "/path/to/file.csv")! let csv = try! CSVReader(stream: stream, codecType: UTF16.self, endian: .big)

Reading a row into a Decodable object

If you have a destination object that conforms to the

Decodable
protocol, you can serialize a row with a new instances of the object.
struct DecodableExample: Decodable {
    let intKey: Int
    let stringKey: String
    let optionalStringKey: String?
}

let csv = """ intKey,stringKey,optionalStringKey 1234,abcd, """

var records = DecodableExample do { let reader = try CSVReader(string: csv, hasHeaderRow: true) let decoder = CSVRowDecoder() while reader.next() != nil { let row = try decoder.decode(DecodableExample.self, from: reader) records.append(row) } } catch { // Invalid row format }

Usage for writing CSV

Write to memory and get a CSV String

NOTE: The default character encoding is

UTF8
.
import Foundation
import CSV

let csv = try! CSVWriter(stream: .toMemory())

// Write a row try! csv.write(row: ["id", "name"])

// Write fields separately csv.beginNewRow() try! csv.write(field: "1") try! csv.write(field: "foo") csv.beginNewRow() try! csv.write(field: "2") try! csv.write(field: "bar")

csv.stream.close()

// Get a String let csvData = csv.stream.property(forKey: .dataWrittenToMemoryStreamKey) as! Data let csvString = String(data: csvData, encoding: .utf8)! print(csvString) // => "id,name\n1,foo\n2,bar"

Write to file

NOTE: The default character encoding is

UTF8
.
import Foundation
import CSV

let stream = OutputStream(toFileAtPath: "/path/to/file.csv", append: false)! let csv = try! CSVWriter(stream: stream)

try! csv.write(row: ["id", "name"]) try! csv.write(row: ["1", "foo"]) try! csv.write(row: ["1", "bar"])

csv.stream.close()

Installation

CocoaPods

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

Carthage

github "yaslab/CSV.swift" ~> 2.4.3

Swift Package Manager

.package(url: "https://github.com/yaslab/CSV.swift.git", .upToNextMinor(from: "2.4.3"))

Reference specification

License

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.