swift : Streaming/Writing out a CSV file

标签 swift csv

我正在使用我的手机记录一些传感器数据并通过 SQLite 通过 SharkORM(DBAccess) 将其存储在设备上。

我现在想将该数据写入 CSV 文件,但是,我现在有多达 160 万条记录。

目前,我正在遍历 1000 条记录,将它们添加到一个字符串中,最后将它们写出。但是,一定有更好的方法吗?

func writeRawFile()
    {
        let fileName = "raw"
        let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

        let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("csv")

        var data = "time,lat,lon,speed,x_acc,y_acc,z_acc,gyro_x,gyro_y,gyro_z,compass,orientation\r\n"

        let count = RawReading.query().count()
        var counter = 0;
        let df = DateFormatter()
        df.dateFormat = "y-MM-dd H:m:ss.SSSS"

        for i in stride(from:0, to: count, by: 1000)
        {
            autoreleasepool {

                for result in RawReading.query().offset(Int32(i)).limit(1000).fetch()
                {
                    if let raw : RawReading = result as? RawReading
                    {
                        if (Double(raw.speed!) > 3.0) //1 Meter per Second = 2.236936 Miles per Hour
                        {
                            //print(df.string(from: raw.date!))

                            data += "\(df.string(from: raw.date!)),\(raw.lat!),\(raw.lon!),\(raw.speed!),\(raw.x!),\(raw.y!),\(raw.z!),\(raw.xx!),\(raw.yy!),\(raw.zz!),\(raw.compass!),\(raw.orientation!)" + "\r\n"

                            counter += 1
                        }

                    }
                }

                print ("Written \(i) of \(count)")

            }
        }

        print("Count \(count) \(counter)")

        //write the file, return true if it works, false otherwise.
        do{
            try data.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8 )
        } catch{
            print("error")
        }
    }

最佳答案

打开FileHandle用于编写,然后分别构建和编写每一行,这样您就不必保留整个文件内容 在内存中:

do {
    let file = try FileHandle(forWritingTo: fileURL)
    defer { file.closeFile() }

    for <... your loop ...> {
        let line = ... // build one CSV line
        file.write(line.data(using: .utf8)!)
    }

} catch let error {
    // ...
}

也可以先写入一个临时文件,然后重命名为 实际文件,以避免文件损坏 出错了。

关于 swift : Streaming/Writing out a CSV file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42578607/

相关文章:

swift - 如何在使用 Swift 3 中的 Measurements 的函数调用中使用泛型

Netlogo 中的 CSV 导出错误

python - 使用循环将 CSV 数据提取到对象中

ios - 如何使用 swift 函数 loadLeaderboardSets WithCompletionHandler

javascript - Node.js 在 API 调用上循环

powershell - 比较powershell中的两个csv条目

python - 按列名对 CSV 进行排序

SwiftUI:重置 .sheet 的状态(_:onDismiss :)

ios - 是否可以在 RealmSwift 中使用枚举?

ios - 如何更改 UIToolbar 高度?