Skip to content
/ csvcore Public

A native Swift library to read and write CSV files

License

Notifications You must be signed in to change notification settings

brampf/csvcore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSVCore

MIT License Swift 5.3 Language Swift

A native Swift library to read and write CSV files

Description

CSVCore is a pure Swift library to read and write files in the CSV file format.

Features

  • Read & Write CSV files
    • Custom delimiters (comma, semicolon)
    • Custom linefeeds (LF, CR, CR_LF)
  • Parsing of native value types
    • Text with individual encodings per column
    • Numbers with individual formats per column using NumberFormatter
    • Dates with individual formats per column using DateFormatter
  • Native code
    • Swift 5.3
    • Compiles for macCatalyst
    • Compiles for iPadOS
    • Compiles for Linux

Getting started

Package Manager

With the swift package manager, add the library to your dependencies

dependencies: [
.package(url: "https://github.com/brampf/csvcore.git", from: "0.1.0")
]

then simply add the CSV import to your target

.target(name: "YourApp", dependencies: ["CSV"])

Documentation

TL;DR

Reading CSV Files

import CSV

/// Parste a "standard" CSV with (,"LF)
let url =  URL("/path/to/some/csv/file")!
let file = try! CSVFile.read(from: url)

Tweaking the details

import CSV

/// Parste a "non standard" CSV with (;"CR_LF)

var config = CSVConfig()
config.eol = .CR_LF
config.delimiter = Character(",").asciiValue!

let url = URL("/path/to/some/csv/file")!
let file = try! CSVFile.read(contentsOf: url, config: config)

Parsing specific value formats

import CSV

/// Parste a "non standard" CSV with (;"CR_LF)

var config = CSVConfig()
config.eol = .CR_LF
config.delimiter = Character(",").asciiValue!

let url = URL("/path/to/some/csv/file")!
let file = try! CSVFile.read(contentsOf: url, config: config)

Writing CSV Files

import CSV

let file = CSVFile(header: [], rows: [[11,12],[21,22],[31,32]])

// spell them out instead of numeric values
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut

var config = CSVConfig()
config.format = [
    FormatSpecifier.Number(format: formatter),
    FormatSpecifier.Number(format: formatter)
]

try! file.write(to: url, config: config)

Tests

There are various test cases implemented to verify compability with a variiety of real world examples of CSV files

License

MIT license; see LICENSE. (c) 2020