gpt4 book ai didi

swift : Streaming/Writing out a CSV file

转载 作者:可可西里 更新时间:2023-11-01 00:35:50 28 4
gpt4 key购买 nike

我正在使用我的手机记录一些传感器数据并通过 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com