A simple, but efficient CSV Parser, written in Swift.

Overview

CI Status Code Coverage Latest Release License Swift Version

CSV

CSV.swift is a powerful swift library for parsing CSV files that supports reading as [String], [String: String] and Decodable, without sacrificing performance.

CSV.swift also supports the new async/await functionality of swift 5.5.

Why?

There are already a few CSV parsers for Swift, but I was not able to find one that had support for high-speed parsing, while still offering convenience features, such as parsing Decodables.

Speed Comparison

[Coming Soon]

Installation

Swift Package Manager

CSV.Swift is compatible with Swift Package Manager. Simply add it to your project's Package.swift.

dependencies: [
	.package(url: "https://github.com/benkoska/csv.swift.git", from: "0.1.0")
],
targets: [
	.target(
		name: "Project",
		dependencies: [
			.product(name: "CSV", package: "csv")
		]
	)
]

After the installation you can import CSV in your .swift files.

import CSV

Docs

Docs avaliable at https://docs.benkoska.com/csv

Usage

let string = "joe,doe,28"
let parser = try CSVParser(url: url, header: ["firstName", "lastName", "age"])

Get next as array

">
parser.next() // => Optional<["joe", "doe", "28"]>

Get next as dictionary

">
try parser.nextAsDict() // => Optional<["firstName": "joe", "lastName": "doe", "age": "28"]>

Decode next from Decodable

">
struct Person: Decodable {
	let firstName: String
	let lastName: String
	let age: Int
}

try parser.next(as: Person.self) // => Optional
   

Create parser from URL

let parser = try CSVParser(url: url)
let parser = try CSVParser(url: url, delimiter: "|")
let parser = try CSVParser(url: url, delimiter: "|", hasHeader: true)
let parser = try CSVParser(url: url, delimiter: "|", header: ["firstName", "lastName", "age"])

Create parser from String

let string = "joe,doe,28\njane,doe,21"
let string2 = "joe|doe|28\njane|doe|21"
let string3 = "firstName|lastName|age\njoe|doe|28\njane|doe|21"

let parser = try CSVParser(string: string)
let parser = try CSVParser(string: string2, delimiter: "|")
let parser = try CSVParser(string: string3, delimiter: "|", hasHeader: true)
let parser = try CSVParser(string: string2, delimiter: "|", header: ["firstName", "lastName", "age"])

Create parser from Data

let parser = try CSVParser(data: data)
let parser = try CSVParser(data: data, delimiter: "|")
let parser = try CSVParser(data: data, delimiter: "|", hasHeader: true)
let parser = try CSVParser(data: data, delimiter: "|", header: ["firstName", "lastName", "age"])

Disclaimer

Until Swift.CSV reaches version 1.0.0 the API is subject to breaking changes between minor version jumps.

The being said, I will try to minimize breaking changes to the public API.

Author

Ben Koska, [email protected]

License

Swift.CSV is available under the MIT license. See the LICENSE file for more info.

You might also like...
HxSTLParser is a basic STL parser capable of loading STL files into an SCNNode

HxSTLParser HxSTLParser is a basic STL parser capable of loading STL files into an SCNNode. Installing Via Carthage Just add it to your Cartfile githu

A result builder that build HTML parser and transform HTML elements to strongly-typed result, inspired by RegexBuilder.

HTMLParserBuilder A result builder that build HTML parser and transform HTML elements to strongly-typed result, inspired by RegexBuilder. Note: Captur

iOS Logs, Events, And Plist Parser

iLEAPP iOS Logs, Events, And Plists Parser Details in blog post here: https://abrignoni.blogspot.com/2019/12/ileapp-ios-logs-events-and-properties.htm

A simple Pokedex app written in Swift that implements the PokeAPI, using Combine and data driven UI.
A simple Pokedex app written in Swift that implements the PokeAPI, using Combine and data driven UI.

SwiftPokedex SwiftPokedex is a simple Pokedex app written by Viktor Gidlöf in Swift that implements the PokeAPI. For full documentation and implementa

Simple and Lightweight App Version Tracking for iOS written in Swift

AEAppVersion Simple and lightweight iOS App Version Tracking written in Swift I made this for personal use, but feel free to use it or contribute. For

Simple getter for bundle informations, written in Swift

BundleInfos Simple getter for bundle informations It's simple and static way for getting information from main bundle Requirements iOS 8.0+ Xcode 7.3

Swift-HorizontalPickerView - Customizable horizontal picker view component written in Swift for UIKit/iOS

Horizontal Picker View Customizable horizontal picker view component written in

A utility that reminds your iPhone app's users to review the app written in pure Swift.
A utility that reminds your iPhone app's users to review the app written in pure Swift.

SwiftRater SwiftRater is a class that you can drop into any iPhone app that will help remind your users to review your app on the App Store/in your ap

An extensible monitoring framework written in Swift

XestiMonitors Overview Reference Documentation Requirements Installation CocoaPods Carthage Swift Package Manager Usage Core Location Monitors Core Mo

Comments
  • Add (very basic) support for quotation

    Add (very basic) support for quotation

    This patch enables parsing data that contains quoted content (which may contain the delimiter or even newlines in a field). By default, the double quote " (0x22) is assumed but a different quotation mark may be specified when initializing the CSVParser.

    Parsing performance takes a small hit because it's necessary to keep checking if the parser is currently in a quotation.

    What's missing:

    • ~~Support for escaped quotation marks inside a quotation~~
    • Probably some more testing :)
    opened by andreasley 1
Releases(0.1.2)
  • 0.1.2(Dec 13, 2021)

  • 0.1.1(Dec 5, 2021)

  • 0.1.0(Dec 5, 2021)

    This release includes the following features:

    • Reading CSV from URL, String and Data
    • Reading CSV with custom delimiter
    • Parsing CSV as [String], [String: String] and Decodable
    • async/await support using AsyncCSVParser, AsyncDictionaryCSVParser and AsyncCodableCSVParser
    Source code(tar.gz)
    Source code(zip)
Owner
Ben Koska
Ben Koska
Taking a string containing a csv file and split it into records (aka lines) containing fields of data (aka Array of SubStrings)

Swift .csv parser Taking a string containing a csv file and split it into records (aka lines) containing fields of data (aka Array of SubStrings). Par

Matthias 0 Dec 29, 2021
Sovran-Swift: Small, efficient, easy. State Management for Swift

Sovran-Swift: Small, efficient, easy. State Management for Swift

Segment 5 Jan 3, 2023
A handy collection of Swift method and Tools to build project faster and more efficient.

SwifterKnife is a collection of Swift extension method and some tools that often use in develop project, with them you might build project faster and

李阳 4 Dec 29, 2022
A Powerful , Extensible CSS Parser written in pure Swift.

A Powerful , Extensible CSS Parser written in pure Swift.

null 273 Sep 9, 2022
A parser combinator library written in the Swift programming language.

SwiftParsec SwiftParsec is a Swift port of the Parsec parser combinator library. It allows the creation of sophisticated parsers from a set of simple

David Dufresne 219 Nov 6, 2022
Swift Property Wrappers, but in Objective-C. And done horribly.

TOPropertyAccessor is an open source, Objective-C abstract class. Similar to Realm's Cocoa API, it uses the dynamic nature of the Objective-C runtime to access the properties of any of its subclasses, and routes calling them through overridable access points.

Tim Oliver 3 May 23, 2021
Swift Parser Combinator library inspired by NimbleParsec for Elixir.

SimpleParsec Simple parser combinator library for Swift inspired by NimbleParsec for Elixir. Each function in the library creates a Parser which can b

null 0 Dec 27, 2021
An SSH config parser library with a fancy API

The SshConfig makes it quick and easy to load, parse, and decode/encode the SSH configs. It also helps to resolve the properties by hostname and use them safely in your apps (thanks for Optional and static types in Swift).

Artem Labazin 8 Nov 25, 2022
.DS_Store file parser/viewer.

.DS_Store file parser/viewer.

JD Gadina 51 Dec 1, 2022
ParserCombinators - String Parser Construction Kit

ParserCombinators provides a set of elementary building blocks for deriving stru

Marcel Tesch 0 Jan 7, 2022