Skip to content

Latest commit

 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSVReader

Read and parse a .csv file which may include values with quotes which contain commas.

This is the regular expression I used:

(?:\"([^\"]*)\",)|(?:([^,]*),)|([^,]*)

Flaw: It does not handle the case of the final value in each line after the last comma having quotation marks.

Usage:

let reader = try CSVReader(url: URL, keys: [String], separator: String? = "\r\n")
let values = try reader.next(10)

Example: With this .csv file:

first,second,third,fourth,fifth
1,2,3,4,5
6,"7,7,7",8,9,10
11,12,13,"14",15
"16,16,16","17,17,17",18,19,20
21,22,23,24,"25,25"
let url = Bundle.module.url(forResource: "csv/counts.csv", withExtension: nil)!
let reader = try CSVReader(url: url, keys: ["first", "second", "third", "fourth", "fifth"], separator: "\n")
let batch1 = try reader.next(lines: 2)
let line1 = batch1[0]
let line2 = batch1[1]

line1["first"] is "1" line2["second"] is "7,7,7"

let batch2 = try reader.next(lines: 1)
let line3 = batch2[0]

line3["fourth"] is "14"

Calling .next with a number greater than the number of lines in the CSV won't throw an exception.

To get all lines, call .all.

To check if there are remaning lines to be read use .hasNext.

If what you want to do is to import a .csv file into your database, check for a command such as COPY for Postgres which can be performed from a terminal or over a connection.

About

CSV Parser Reader for Swift

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages