Giter VIP home page Giter VIP logo

swift-csv's Introduction

swift-csv

swift-csv is a stream based CSV library written in Swift. It uses InputStream to parse a CSV file and OutputStream to write CSV data to a file. This way it doesn't keep everything in memory while working with big CSV files. It also supports BOM for UTF-8, UTF-16 and UTF-32 text encodings.

swift-csv is battle tested in the CSV import and export of Finances.

Features

  • Stream based CSV parser and writer
  • Complete documentation
  • Unit tested

Usage

Parser

let stream = InputStream(...)

// Define the delimeter and encoding of the CSV file
let configuration = CSV.Configuration(delimiter: ",", encoding: .utf8)

let parser = CSV.Parser(inputStream: stream, configuration: configuration)
try parser.parse()

If you don't know the delimiter and encoding of the data, you can automatically detect it.

let url = ...
guard let configuration = CSV.Configuration.detectConfigurationForContentsOfURL(url) else {
	return
}

Example

Parsing this CSV file

a,b
1,"ha ""ha"" ha"
3,4

will result in the following delegate calls.

parserDidBeginDocument
parser(_:didBeginLineAt:) 0
parser(_:didReadFieldAt:value:) a
parser(_:didReadFieldAt:value:) b
parser(_:didEndLineAt:) 0
parser(_:didBeginLineAt:) 1
parser(_:didReadFieldAt:value:) 1
parser(_:didReadFieldAt:value:) ha "ha" ha
parser(_:didEndLineAt:) 1
parser(_:didBeginLineAt:) 2
parser(_:didReadFieldAt:value:) 3
parser(_:didReadFieldAt:value:) 4
parser(_:didEndLineAt:) 2
parserDidEndDocument

Writer

let stream = OutputStream(...)
let configuration = CSV.Configuration(delimiter: ",", encoding: .utf8)

let writer = CSV.Writer(outputStream: stream, configuration: configuration)
try writer.writeLine(of: ["a", "b", "c"])
try writer.writeLine(of: ["1", "2", "3"])

The code above produces the following output.

a,b,c
1,2,3

TODOs

  • Support comments in CSV file

Contact

Matthias Hochgatterer

Github: https://github.com/brutella/

Twitter: https://twitter.com/brutella

License

swift-csv is available under the MIT license. See the LICENSE file for more info.

swift-csv's People

Contributors

brutella avatar jakob avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.