SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extension String {
  2.    
  3.     var trimmed: String {
  4.         return self.trimmingCharacters(in: .whitespacesAndNewlines)
  5.     }
  6.    
  7. }
  8.  
  9. enum Errors: Error {
  10.     case filePathFailed
  11.     case contentDataFailed
  12.     case contentStringFailed
  13. }
  14.  
  15. func readFile() throws {
  16.     guard let filePath = Bundle.main.path(forResource:"SLP2 - Copy", ofType: "csv")
  17.         else { throw Errors.filePathFailed }
  18.     guard let contentData = FileManager.default.contents(atPath: filePath)
  19.         else { throw Errors.contentDataFailed }
  20.     guard let contentString = String(data: contentData, encoding: .utf8)
  21.         else { throw Errors.contentStringFailed }
  22.     let lines = contentString.components(separatedBy: "\n")
  23.     print("Line Count: \(lines.count)")
  24.    
  25.     let dateFormatter_0 = DateFormatter()
  26.     dateFormatter_0.calendar = Calendar.current
  27.     dateFormatter_0.locale = Locale.current
  28.     dateFormatter_0.dateFormat = "M/d/yy H:mm"
  29.    
  30.     let dateFormatter_1 = DateFormatter()
  31.     dateFormatter_1.calendar = Calendar.current
  32.     dateFormatter_1.locale = Locale.current
  33.     dateFormatter_1.dateFormat = "M/d/yy HH:mm:ss"
  34.    
  35.     let dateFormatter_2 = DateFormatter()
  36.     dateFormatter_2.calendar = Calendar.current
  37.     dateFormatter_2.locale = Locale.current
  38.     dateFormatter_2.dateFormat = "yyyy-MM-dd HH:mm"
  39.    
  40.     let finalString = lines
  41.         .filter { (line) -> Bool in !line.trimmed.isEmpty }
  42.         .map { (line) -> [String] in return line.components(separatedBy: "\",") }
  43.         .map { (array) -> (start: String, end: String) in return (array[0] + "\"", array[1].trimmed) }
  44.         .compactMap { (item) -> (text: String, date: Date)? in
  45.             let d = dateFormatter_0.date(from: item.end) ?? dateFormatter_1.date(from: item.end)
  46.             guard let date = d else { return nil }
  47.             return (item.start, date)
  48.         }
  49.         .map { (item) -> String in return "\(item.text),\(dateFormatter_2.string(from: item.date))" }
  50.         .joined(separator: "\n")
  51.    
  52.     let fileUrl = URL(fileURLWithPath: filePath)
  53.     let finalFileUrl = fileUrl
  54.         .deletingLastPathComponent()
  55.         .appendingPathComponent("FinalCSVFile.csv")
  56.    
  57.     do {
  58.         print("Start Writing File...")
  59.         try ("kwh, Datetime" + "\n" + finalString).write(to: finalFileUrl, atomically: false, encoding: .utf8)
  60.         print("File Created: \(finalFileUrl.absoluteString)")
  61.     } catch {
  62.         print("Write String to File Failed: \(error)")
  63.     }
  64. }
  65.  
  66. do {
  67.     try readFile()
  68. } catch {
  69.     print("Error: \(error as! Errors)")
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top