All Projects β†’ swiftcsv β†’ Swiftcsv

swiftcsv / Swiftcsv

Licence: mit
CSV parser for Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftcsv

Pxi
🧚 pxi (pixie) is a small, fast, and magical command-line data processor similar to jq, mlr, and awk.
Stars: ✭ 248 (-51.47%)
Mutual labels:  csv, tsv, parser
Sqawk
Like Awk but with SQL and table joins
Stars: ✭ 263 (-48.53%)
Mutual labels:  csv, tsv
qwery
A SQL-like language for performing ETL transformations.
Stars: ✭ 28 (-94.52%)
Mutual labels:  tsv, csv
Sq
swiss-army knife for data
Stars: ✭ 275 (-46.18%)
Mutual labels:  csv, tsv
RecordParser
Zero Allocation Writer/Reader Parser for .NET Core
Stars: ✭ 155 (-69.67%)
Mutual labels:  tsv, csv
YouPlot
A command line tool that draw plots on the terminal.
Stars: ✭ 412 (-19.37%)
Mutual labels:  tsv, csv
Flatfiles
Reads and writes CSV, fixed-length and other flat file formats with a focus on schema definition, configuration and speed.
Stars: ✭ 275 (-46.18%)
Mutual labels:  csv, tsv
Visidata
A terminal spreadsheet multitool for discovering and arranging data
Stars: ✭ 4,606 (+801.37%)
Mutual labels:  csv, tsv
Choetl
ETL Framework for .NET / c# (Parser / Writer for CSV, Flat, Xml, JSON, Key-Value, Parquet, Yaml, Avro formatted files)
Stars: ✭ 372 (-27.2%)
Mutual labels:  csv, parser
Csv Parser
A modern C++ library for reading, writing, and analyzing CSV (and similar) files.
Stars: ✭ 359 (-29.75%)
Mutual labels:  csv, parser
tabular-stream
Detects tabular data (spreadsheets, dsv or json, 20+ different formats) and emits normalized objects.
Stars: ✭ 34 (-93.35%)
Mutual labels:  tsv, csv
Pytablewriter
pytablewriter is a Python library to write a table in various formats: CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
Stars: ✭ 422 (-17.42%)
Mutual labels:  csv, tsv
Miller
Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON
Stars: ✭ 4,633 (+806.65%)
Mutual labels:  csv, tsv
jupyterlab-spreadsheet-editor
JupyterLab spreadsheet editor for tabular data (e.g. csv, tsv)
Stars: ✭ 72 (-85.91%)
Mutual labels:  tsv, csv
Sqlparser
Simple SQL parser meant for querying CSV files
Stars: ✭ 249 (-51.27%)
Mutual labels:  csv, parser
Node Csv
Full featured CSV parser with simple api and tested against large datasets.
Stars: ✭ 3,068 (+500.39%)
Mutual labels:  csv, parser
Intellij Csv Validator
CSV validator, highlighter and formatter plugin for JetBrains Intellij IDEA, PyCharm, WebStorm, ...
Stars: ✭ 198 (-61.25%)
Mutual labels:  csv, tsv
Data Curator
Data Curator - share usable open data
Stars: ✭ 199 (-61.06%)
Mutual labels:  csv, tsv
Rainbow csv
🌈Rainbow CSV - Vim plugin: Highlight columns in CSV and TSV files and run queries in SQL-like language
Stars: ✭ 337 (-34.05%)
Mutual labels:  csv, tsv
Stream Parser
⚑ PHP7 / Laravel Multi-format Streaming Parser
Stars: ✭ 391 (-23.48%)
Mutual labels:  csv, parser

SwiftCSV

Swift 5.3 Platform support Build Status Code coverage status CocoaPods Compatible Carthage compatible License MIT Reviewed by Hound

Simple CSV parsing for macOS, iOS, tvOS, and watchOS.

Usage

CSV content can be loaded using the CSV class:

import SwiftCSV

do {
    // As a string
    let csv: CSV = try CSV(string: "id,name,age\n1,Alice,18")

    // With a custom delimiter character
    let tsv: CSV = try CSV(string: "id\tname\tage\n1\tAlice\t18", delimiter: "\t")

    // From a file (with errors)
    let csvFile: CSV = try CSV(url: URL(fileURLWithPath: "path/to/users.csv"))

    // From a file inside the app bundle, with a custom delimiter, errors, and custom encoding
    let resource: CSV? = try CSV(
        name: "users",
        extension: "tsv",
        bundle: .main,
        delimiter: "\t",
        encoding: .utf8)
} catch parseError as CSVParseError {
    // Catch errors from parsing invalid formed CSV
} catch {
    // Catch errors from trying to load files
}

API

If you don't care about accessing named columns, you can set the loadColumns argument to false and the columns Dictionary will not be populated. This can increase performance in critical cases for lots of data.

class CSV {
    /// Load CSV data from a string.
    ///
    /// - parameter string: CSV contents to parse.
    /// - parameter delimiter: Character used to separate  row and header fields (default is ',')
    /// - parameter loadColumns: Whether to populate the `columns` dictionary (default is `true`)
    /// - throws: `CSVParseError` when parsing `string` fails.
    public init(string: String,
                delimiter: Character = comma,
                loadColumns: Bool = true) throws

    /// Load a CSV file as a named resource from `bundle`.
    ///
    /// - parameter name: Name of the file resource inside `bundle`.
    /// - parameter ext: File extension of the resource; use `nil` to load the first file matching the name (default is `nil`)
    /// - parameter bundle: `Bundle` to use for resource lookup (default is `.main`)
    /// - parameter delimiter: Character used to separate row and header fields (default is ',')
    /// - parameter encoding: encoding used to read file (default is `.utf8`)
    /// - parameter loadColumns: Whether to populate the columns dictionary (default is `true`)
    /// - throws: `CSVParseError` when parsing the contents of the resource fails, or file loading errors.
    /// - returns: `nil` if the resource could not be found
    public convenience init?(
        name: String,
        extension ext: String? = nil,
        bundle: Bundle = .main,
        delimiter: Character = comma,
        encoding: String.Encoding = .utf8,
        loadColumns: Bool = true) throws

    /// Load a CSV file from `url`.
    ///
    /// - parameter url: URL of the file (will be passed to `String(contentsOfURL:encoding:)` to load)
    /// - parameter delimiter: Character used to separate row and header fields (default is ',')
    /// - parameter encoding: Character encoding to read file (default is `.utf8`)
    /// - parameter loadColumns: Whether to populate the columns dictionary (default is `true`)
    /// - throws: `CSVParseError` when parsing the contents of `url` fails, or file loading errors.
    public convenience init(
        url: URL,
        delimiter: Character = comma,
        encoding: String.Encoding = .utf8,
        loadColumns: Bool = true)
}

public enum CSVParseError: Error {
    case generic(message: String)
    case quotation(message: String)
}

Reading Data

let csv = CSV(string: "id,name,age\n1,Alice,18\n2,Bob,19")
csv.header         //=> ["id", "name", "age"]
csv.namedRows      //=> [["id": "1", "name": "Alice", "age": "18"], ["id": "2", "name": "Bob", "age": "19"]]
csv.namedColumns   //=> ["id": ["1", "2"], "name": ["Alice", "Bob"], "age": ["18", "19"]]

The rows can also parsed and passed to a block on the fly, reducing the memory needed to store the whole lot in an array:

// Access each row as an array (array not guaranteed to be equal length to the header)
csv.enumerateAsArray { array in
    print(array.first)
}
// Access them as a dictionary
csv.enumerateAsDict { dict in
    print(dict["name"])
}

Installation

CocoaPods

pod "SwiftCSV"

Carthage

github "swiftcsv/SwiftCSV"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].