SwiftCSV
vacy Manifest
简单的CSV解析MacOS,iOS,TVOS和WatchOS。
CSV内容可以使用CSV类加载:
import SwiftCSV
do {
// As a string, guessing the delimiter
let csv : CSV = try CSV < Named > ( string : " id,name,age n 1,Alice,18 " )
// Specifying a custom delimiter
let tsv : CSV = try CSV < Enumerated > ( string : " id t name t age n 1 t Alice t 18 " , delimiter : . tab )
// From a file (propagating error during file loading)
let csvFile : CSV = try CSV < Named > ( url : URL ( fileURLWithPath : " path/to/users.csv " ) )
// From a file inside the app bundle, with a custom delimiter, errors, and custom encoding.
// Note the result is an optional.
let resource : CSV ? = try CSV < Named > (
name : " users " ,
extension : " tsv " ,
bundle : . main ,
delimiter : . character ( " ? " ) , // Any character works!
encoding : . utf8 )
} catch parseError as CSVParseError {
// Catch errors from parsing invalid CSV
} catch {
// Catch errors from trying to load files
}CSV类带有适用于从URL加载文件的初始化器。
extension CSV {
/// Load a CSV file from `url`.
///
/// - Parameters:
/// - url: URL of the file (will be passed to `String(contentsOfURL:encoding:)` to load)
/// - delimiter: Character used to separate separate cells from one another in rows.
/// - encoding: Character encoding to read file (default is `.utf8`)
/// - 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 : CSVDelimiter ,
encoding : String . Encoding = . utf8 ,
loadColumns : Bool = true ) throws
/// Load a CSV file from `url` and guess its delimiter from `CSV.recognizedDelimiters`, falling back to `.comma`.
///
/// - Parameters:
/// - url: URL of the file (will be passed to `String(contentsOfURL:encoding:)` to load)
/// - encoding: Character encoding to read file (default is `.utf8`)
/// - 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 ,
encoding : String . Encoding = . utf8 ,
loadColumns : Bool = true )
}分界符是强烈键入的。公认的CSVDelimiter病例是: .comma , .semicolon和.tab 。
您可以使用便利性初始化器为您猜测定界符的识别列表。这些初始化器可用于从URL和字符串加载CSV。
加载CSV数据时,您也可以使用任何其他单字符定界符。诸如"x"之类的字符将产生CSV.Delimiter.character("x") ,因此您不必键入整个.character(_)案例名称。每个变体都有一个接受明确定界符设置的初始化器。
// Recognized the comma delimiter automatically:
let csv = CSV < Named > ( string : " id,name,age n 1,Alice,18 n 2,Bob,19 " )
csv . header //=> ["id", "name", "age"]
csv . rows //=> [["id": "1", "name": "Alice", "age": "18"], ["id": "2", "name": "Bob", "age": "19"]]
csv . columns //=> ["id": ["1", "2"], "name": ["Alice", "Bob"], "age": ["18", "19"]]这些行还可以解析并传递到飞行中的一个街区,从而减少了将整个批次存储在数组中所需的内存:
// Access each row as an array (inner array not guaranteed to always 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 " ] )
}使用CSV<Named> aka NamedCSV以列指定列的列基础访问CSV数据。认为这是一个横截面:
let csv = NamedCSV ( string : " id,name,age n 1,Alice,18 n 2,Bob,19 " )
csv . rows [ 0 ] [ " name " ] //=> "Alice"
csv . columns [ " name " ] //=> ["Alice", "Bob"]如果您只想访问数据行,而不是副柱子,则可以使用CSV<Enumerated>或EnumeratedCSV :
let csv = EnumeratedCSV ( string : " id,name,age n 1,Alice,18 n 2,Bob,19 " )
csv . rows [ 0 ] [ 1 ] //=> "Alice"
csv . columns ? [ 0 ] . header //=> "name"
csv . columns ? [ 0 ] . rows //=> ["Alice", "Bob"]为了加快速度,通过通过loadColumns: false 。这将防止柱状数据被填充。对于大型数据集,这节省了很多迭代(在二次运行时)。
let csv = EnumeratedCSV ( string : " id,name,age n 1,Alice,18 n 2,Bob,19 " , loadColumns : false )
csv . rows [ 0 ] [ 1 ] //=> "Alice"
csv . columns //=> nil pod "SwiftCSV" github "swiftcsv/SwiftCSV"
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.8.0")
该包具有空隐私清单,因为它无法访问或跟踪任何敏感数据。