Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

14 Commits

Repository files navigation

CSVCore

MIT LicenseSwift 5.3LanguageSwift

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)
leturl=URL("/path/to/some/csv/file")!
letfile=try!CSVFile.read(from: url)

Tweaking the details

import CSV
/// Parste a "non standard" CSV with (;"CR_LF)
varconfig=CSVConfig()
config.eol =.CR_LF
config.delimiter =Character(",").asciiValue!
leturl=URL("/path/to/some/csv/file")!
letfile=try!CSVFile.read(contentsOf: url, config: config)

Parsing specific value formats

import CSV
/// Parste a "non standard" CSV with (;"CR_LF)
varconfig=CSVConfig()
config.eol =.CR_LF
config.delimiter =Character(",").asciiValue!
leturl=URL("/path/to/some/csv/file")!
letfile=try!CSVFile.read(contentsOf: url, config: config)

Writing CSV Files

import CSV
letfile=CSVFile(header:[], rows:[[11,12],[21,22],[31,32]])
// spell them out instead of numeric values
letformatter=NumberFormatter()
formatter.numberStyle =.spellOut
varconfig=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

Releases

Packages

Contributors

Languages