-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
keba76
authored and
keba76
committed
Nov 2, 2016
0 parents
commit f9a2f36
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// CSV | ||
// Modified for Swift 3 | ||
// | ||
|
||
import Foundation | ||
|
||
open class CSV { | ||
open var headers: [String] = [] | ||
open var rows: [Dictionary<String, String>] = [] | ||
open var columns = Dictionary<String, [String]>() | ||
var delimiter = CharacterSet(charactersIn: ",") | ||
|
||
public init(content: String?, delimiter: CharacterSet, encoding: UInt) throws{ | ||
if let csvStringToParse = content{ | ||
self.delimiter = delimiter | ||
|
||
let newline = CharacterSet.newlines | ||
var lines: [String] = [] | ||
csvStringToParse.trimmingCharacters(in: newline).enumerateLines { line, stop in lines.append(line) } | ||
|
||
self.headers = self.parseHeaders(fromLines: lines) | ||
self.rows = self.parseRows(fromLines: lines) | ||
self.columns = self.parseColumns(fromLines: lines) | ||
} | ||
} | ||
// start off | ||
public convenience init(contentsOfURL url: String) throws { | ||
let comma = CharacterSet(charactersIn: ",") | ||
let csvString: String? | ||
do { | ||
csvString = try String(contentsOfFile: url, encoding: String.Encoding.utf8) | ||
} catch _ { | ||
csvString = nil | ||
}; | ||
try self.init(content: csvString,delimiter:comma, encoding:String.Encoding.utf8.rawValue) | ||
} | ||
|
||
|
||
func parseHeaders(fromLines lines: [String]) -> [String] { | ||
return lines[0].components(separatedBy: self.delimiter) | ||
} | ||
|
||
func parseRows(fromLines lines: [String]) -> [Dictionary<String, String>] { | ||
var rows: [Dictionary<String, String>] = [] | ||
|
||
for (lineNumber, line) in lines.enumerated() { | ||
if lineNumber == 0 { | ||
continue | ||
} | ||
|
||
var row = Dictionary<String, String>() | ||
let values = line.components(separatedBy: self.delimiter) | ||
for (index, header) in self.headers.enumerated() { | ||
if index < values.count { | ||
row[header] = values[index] | ||
} else { | ||
row[header] = "" | ||
} | ||
} | ||
rows.append(row) | ||
} | ||
|
||
return rows | ||
} | ||
|
||
func parseColumns(fromLines lines: [String]) -> Dictionary<String, [String]> { | ||
var columns = Dictionary<String, [String]>() | ||
|
||
for header in self.headers { | ||
let column = self.rows.map { row in row[header] != nil ? row[header]! : "" } | ||
columns[header] = column | ||
} | ||
|
||
return columns | ||
} | ||
} |