CSVKit

1.0

CSVKit is a very simple CSV parsing library for Swift programming
sanzaru/csvkit

What's New

1.0

2021-01-03T21:21:27Z

CSVKit

Build Status

CSVKit is a very simple, fast and lightweight CSV parsing and encoding library for Swift programming.

The parser reads in an String or Data object and parses the CSV fields into an array of strings. Every root element corresponds to one line of the data.

Features:

  • Parse CSV from String and Data
  • Encode string arrays to CSV string data

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/sanzaru/csvkit.git", from: "0.0.1")
]

Example usage

Decoding:

Default decoding

import Foundation
import CSVKit

struct App {
    static func main() {
        // Decode with semicolon (default) separator
        let csvData = "Foo;Bar;Foo2;Bar2\n\"Line2\";\"Line2-1\";\"Line2-2\";\"Line2-3\""
        let parsed = CSVParser.shared.parse(from: csvData)
        dump(parsed)
    }
}

App.main()

Output:

▿ 2 elements
  ▿ 4 elements
    - "Foo"
    - "Bar"
    - "Foo2"
    - "Bar2"
  ▿ 4 elements
    - "Line2"
    - "Line2-1"
    - "Line2-2"
    - "Line2-3"

Custom separator

import Foundation
import CSVKit

struct App {
    static func main() {
        // Decode with custom separator
        let csvDataComma = "Foo,Bar,Foo2,Bar2\n\"Line2\",\"Line2-1\",\"Line2-2\",\"Line2-3\""
        
        var parser = CSVParser()
        parser.separator = ","
        
        let parsedComma = parser.parse(from: csvDataComma)
        dump(parsedComma)
    }
}

App.main()

Output:

▿ 2 elements
  ▿ 4 elements
    - "Foo"
    - "Bar"
    - "Foo2"
    - "Bar2"
  ▿ 4 elements
    - "Line2"
    - "Line2-1"
    - "Line2-2"
    - "Line2-3"

Encoding:

import Foundation
import CSVKit

struct App {        
    static func main() {
        do {
            let dummyData = "Foo;Bar;Foo2;Bar2\n\"Line2\";\"Line2-1\";\"Line2-2\";\"Line2-3\""
            
            let csvData: [[String]] = CSVParser.shared.parse(from: dummyData) 
            dump(try CSVEncoder.shared.encode(from: csvData))
        } catch {
            fatalError(error.localizedDescription)
        }
    }
}

App.main()

Output:

    "Foo;Bar;Foo2;Bar2\r\nLine2;\"Line2-1\";\"Line2-2\";\"Line2-3\""

Changelog

Version 0.0.5

- Fixed encoding validation
- Extended unit tests

Version 0.0.4

- Better standard conformance (RFC 4180)

Version 0.0.3

- Added encoding functionality
- New function parameters and separator handling

Version 0.0.2

- Bugfixes

Version 0.0.1

- First release

License

CSVKit is released under the Apache License 2.0

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Oct 20 2024 20:07:34 GMT-0900 (Hawaii-Aleutian Daylight Time)