0

I am trying to read the content of a .csv file using the framework CreateML to read csv data.

Fhe following code generates an error even though the file exists:

let csvURL = URL(fileURLWithPath: "/Volumes/MAC HDD/Data/Data.csv")
let fm = FileManager()
if (fm.fileExists(atPath: csvURL.path)) {
    let dataTable = try! MLDataTable(contentsOf: csvURL)
    // accessing first column
    let col_1 = Array.init(dataTable["col1"])
}

I get the following error message:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: CreateML.MLCreateError.generic(reason: "No files corresponding to the specified path (file:///Volumes/MAC%20HDD/Data/Data.csv)")

I have checked nearly everything but can't get any results. What am I doing wrong?

J.E.K
  • 1,321
  • 10
  • 17
  • What happens if there is no space in the path? What happens if you put a backslash before the space? `"/Volumes/MAC\ HDD/Data/Data.csv"` – Yonat Dec 03 '18 at 21:09
  • That gives an error of `invalid escape sequence in literal`. – J.E.K Dec 03 '18 at 21:49
  • Ah right, you need to escape the backslash: `"/Volumes/MAC\\ HDD/Data/Data.csv"` – Yonat Dec 05 '18 at 06:48
  • That gives me also an error (file does not exist). I think whitespace isn't the problem at all, because `fileURLWithPath` gives me a valid file url that is used to check if the file exists. I have tried another volume without any space in the path and still get the same error message. – J.E.K Dec 05 '18 at 10:56

1 Answers1

1

I found out by my self what the problem is.

I have implemented this code in a Xcode project to read csv data, but the CreateML framework is just working for Xcode Playground and not within a Xcode Project! It was mentioned at the end of the WWDC 2018 session video 703.

The code example above is working fine with Xcode Playground.

It would have saved me a lot of time if there had been a warning when importing the framework.

J.E.K
  • 1,321
  • 10
  • 17
  • I can't get 'MLDataTable' to read a json file in either, not outside of a playground (March '19). It always acts as if the file isn't there, or is empty (you get the same result with a bad filename). Like you, I wish there had been a warning - I spent most of the day today convinced I was being dumb. Works great in Playgrounds, nada in a desktop 'app'. – robsoft Mar 07 '19 at 20:29