A native Swift library to read and write CSV files
CSVCore is a pure Swift library to read and write files in the CSV file format.
- 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
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"])
import CSV
/// Parste a "standard" CSV with (,"LF)
let url = URL("/path/to/some/csv/file")!
let file = try! CSVFile.read(from: url)
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)
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)
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)
There are various test cases implemented to verify compability with a variiety of real world examples of CSV files
MIT license; see LICENSE. (c) 2020