All Projects → yaslab → Csv.swift

yaslab / Csv.swift

Licence: mit
CSV reading and writing library written in Swift.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Csv.swift

Bytearray.js
An equivalent to Actionscript 3's ByteArray for Javascript with AMF0 and AMF3 support.
Stars: ✭ 100 (-80.69%)
Mutual labels:  reading, writing
Rwtxt
A cms for absolute minimalists.
Stars: ✭ 914 (+76.45%)
Mutual labels:  reading, writing
Readteractive
Tool for writing and generating interactive books.
Stars: ✭ 23 (-95.56%)
Mutual labels:  reading, writing
eureka
✍️ I read, I write, I think, I do, I learn, I code.
Stars: ✭ 106 (-79.54%)
Mutual labels:  writing, reading
Rio
A Swiss-Army Knife for Data I/O
Stars: ✭ 467 (-9.85%)
Mutual labels:  csv
Bsed
Simple SQL-like syntax on top of Perl text processing.
Stars: ✭ 414 (-20.08%)
Mutual labels:  csv
Mockneat
MockNeat is a Java 8+ library that facilitates the generation of arbitrary data for your applications.
Stars: ✭ 410 (-20.85%)
Mutual labels:  csv
Specs
Technical specifications and guidelines for implementing Frictionless Data.
Stars: ✭ 403 (-22.2%)
Mutual labels:  csv
Graphite
Encrypted, secure, user-owned productivity suite
Stars: ✭ 505 (-2.51%)
Mutual labels:  writing
Vim Colors Pencil
Light (& dark) color scheme inspired by iA Writer
Stars: ✭ 498 (-3.86%)
Mutual labels:  writing
Es2csv
Export from an Elasticsearch into a CSV file
Stars: ✭ 465 (-10.23%)
Mutual labels:  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 (-18.53%)
Mutual labels:  csv
Bookwyrm
Social reading and reviewing, decentralized with ActivityPub
Stars: ✭ 483 (-6.76%)
Mutual labels:  reading
Alex
Catch insensitive, inconsiderate writing
Stars: ✭ 4,124 (+696.14%)
Mutual labels:  writing
Csvutil
csvutil provides fast and idiomatic mapping between CSV and Go (golang) values.
Stars: ✭ 501 (-3.28%)
Mutual labels:  csv
Easy localization
Easy and Fast internationalizing your Flutter Apps
Stars: ✭ 407 (-21.43%)
Mutual labels:  csv
Vroom
Fast reading of delimited files
Stars: ✭ 462 (-10.81%)
Mutual labels:  csv
Things.sh
Simple read-only comand-line interface to your Things 3 database
Stars: ✭ 492 (-5.02%)
Mutual labels:  csv
Private Ai Resources
SOON TO BE DEPRECATED - Private machine learning progress
Stars: ✭ 461 (-11%)
Mutual labels:  writing
Pandoc Starter
📄 My pandoc markdown templates and makefiles
Stars: ✭ 443 (-14.48%)
Mutual labels:  writing

CSV.swift

Build Status codecov Open Source Helpers

CSV reading and writing library written in Swift.

Usage for reading CSV

From string

import CSV

let csvString = "1,foo\n2,bar"
let csv = try! CSVReader(string: csvString)
while let row = csv.next() {
    print("\(row)")
}
// => ["1", "foo"]
// => ["2", "bar"]

From file

NOTE: The default character encoding is UTF8.

import Foundation
import CSV

let stream = InputStream(fileAtPath: "/path/to/file.csv")!
let csv = try! CSVReader(stream: stream)
while let row = csv.next() {
    print("\(row)")
}

Getting the header row

import CSV

let csvString = "id,name\n1,foo\n2,bar"
let csv = try! CSVReader(string: csvString,
                         hasHeaderRow: true) // It must be true.

let headerRow = csv.headerRow!
print("\(headerRow)") // => ["id", "name"]

while let row = csv.next() {
    print("\(row)")
}
// => ["1", "foo"]
// => ["2", "bar"]

Get the field value using subscript

import CSV

let csvString = "id,name\n1,foo"
let csv = try! CSVReader(string: csvString,
                         hasHeaderRow: true) // It must be true.

while csv.next() != nil {
    print("\(csv["id"]!)")   // => "1"
    print("\(csv["name"]!)") // => "foo"
}

Provide the character encoding

If you use a file path, you can provide the character encoding to initializer.

import Foundation
import CSV

let stream = InputStream(fileAtPath: "/path/to/file.csv")!
let csv = try! CSVReader(stream: stream,
                         codecType: UTF16.self,
                         endian: .big)

Reading a row into a Decodable object

If you have a destination object that conforms to the Decodable protocol, you can serialize a row with a new instances of the object.

struct DecodableExample: Decodable {
    let intKey: Int
    let stringKey: String
    let optionalStringKey: String?
}

let csv = """
    intKey,stringKey,optionalStringKey
    1234,abcd,
    """

var records = [DecodableExample]()
do {
    let reader = try CSVReader(string: csv, hasHeaderRow: true)
    let decoder = CSVRowDecoder()
    while reader.next() != nil {
        let row = try decoder.decode(DecodableExample.self, from: reader)
        records.append(row)
    }
} catch {
    // Invalid row format
}

Usage for writing CSV

Write to memory and get a CSV String

NOTE: The default character encoding is UTF8.

import Foundation
import CSV

let csv = try! CSVWriter(stream: .toMemory())

// Write a row
try! csv.write(row: ["id", "name"])

// Write fields separately
csv.beginNewRow()
try! csv.write(field: "1")
try! csv.write(field: "foo")
csv.beginNewRow()
try! csv.write(field: "2")
try! csv.write(field: "bar")

csv.stream.close()

// Get a String
let csvData = csv.stream.property(forKey: .dataWrittenToMemoryStreamKey) as! Data
let csvString = String(data: csvData, encoding: .utf8)!
print(csvString)
// => "id,name\n1,foo\n2,bar"

Write to file

NOTE: The default character encoding is UTF8.

import Foundation
import CSV

let stream = OutputStream(toFileAtPath: "/path/to/file.csv", append: false)!
let csv = try! CSVWriter(stream: stream)

try! csv.write(row: ["id", "name"])
try! csv.write(row: ["1", "foo"])
try! csv.write(row: ["1", "bar"])

csv.stream.close()

Installation

CocoaPods

pod 'CSV.swift', '~> 2.4.3'

Carthage

github "yaslab/CSV.swift" ~> 2.4.3

Swift Package Manager

.package(url: "https://github.com/yaslab/CSV.swift.git", .upToNextMinor(from: "2.4.3"))

Reference specification

License

CSV.swift is released under the MIT license. See the LICENSE file for more info.

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].