การแยกวิเคราะห์ 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 มาพร้อมกับ Initializers ที่เหมาะสำหรับการโหลดไฟล์จาก 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
คุณสามารถใช้ Initializers ความสะดวกสบายที่คาดเดาตัวคั่นจากรายการที่เป็นที่รู้จักสำหรับคุณ ผู้เริ่มต้นเหล่านี้มีให้สำหรับการโหลด CSV จาก URL และสตริง
นอกจากนี้คุณยังสามารถใช้ตัวคั่นตัวอักษรเดี่ยวอื่น ๆ เมื่อโหลดข้อมูล 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")
แพ็คเกจจัดส่งด้วยความเป็นส่วนตัวที่ว่างเปล่าเพราะไม่สามารถเข้าถึงหรือติดตามข้อมูลที่ละเอียดอ่อนใด ๆ