在Swift中解析CSV文件 [英] Parsing CSV file in Swift
本文介绍了在Swift中解析CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当应用程式启动时,我需要预先载入资料到我的tableView。所以我使用核心数据通过解析.csv文件。为此,我将遵循本教程。
这是我的parseCSV函数
func parseCSV(contentsOfURL:NSURL,encoding:NSStringEncoding,error:NSErrorPointer) - > ; [(stationName:String,stationType:String,stationLineType:String,stationLatitude:String,stationLongitude:String)]? {
//加载CSV文件并解析它
let delimiter =,
var stations:[(stationName:String,stationType:String,stationLineType:String,stationLatitude:String,stationLongitude :String)]?
let content = String(contentsOfURL:contentsOfURL,encoding:encoding,error:error)
stations = []
let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet。
如果行!={
/ b)$ line $ = /对于有双引号的行
//我们使用NSScanner执行解析
如果line.rangeOfString(\)!= nil {
var textToScan:String = line
var value:NSString?
var textScanner:NSScanner = NSScanner(string:textToScan)
while textScanner.string!={
if(textScanner.string as NSString).substringToIndex(1)==\{
textScanner.scanLocation + = 1
textScanner.scanUpToString(\,intoString:& value)
textScanner .scanLocation + = 1
} else {
textScanner.scanUpToString(delimiter,intoString:& value)
}
//将值存储到values数组
values.append(value as! String)
//检索字符串的未扫描的剩余部分
if textScanner.scanLocation< textScanner.string.characters.count {
textToScan =(textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1)
} else {
textToScan =
}
textScanner = NSScanner(string:textToScan)
}
//对于没有双引号的行,我们可以使用分隔符分隔字符串
// (例如逗号)
} else {
values = line.componentsSeparatedByString(delimiter)
}
//将值放入元组并将其添加到项array
let station =(stationName:values [0],stationType:values [1],stationLineType:values [2],stationLatitude:values [3],stationLongitude:values [4] .append(station)
}
}
返回站
}
这是我的示例.csv文件
Rithala ,28.7209,77.1070
但我在此行上收到错误
let station =(stationName:values [0],stationType:values [1],stationLineType:values [2],stationLatitude:values [3],stationLongitude:值[4])
station?.append(station)
致命错误:数组索引超出范围
我做错了什么?请帮助我。
解决方案
您正在尝试解析文件路径而不是文件的内容
如果替换
let content = String(contentsOfURL:contentOfURL,encoding:encoding,error :error)
其中:
if let data = NSData(contentsOfURL:contentOfURL){
if let content = NSString(data:data,encoding:NSUTF8StringEncoding){
// existing code
}
}
那么该代码将适用于您的示例文件。
I need to preload data into my tableView when the app launches. So i am using core data by parsing a .csv file. I am following this tutorial for this purpose. Here is my parseCSV function
func parseCSV (contentsOfURL: NSURL, encoding: NSStringEncoding, error: NSErrorPointer) -> [(stationName:String, stationType:String, stationLineType: String, stationLatitude: String, stationLongitude: String)]? {
// Load the CSV file and parse it
let delimiter = ","
var stations:[(stationName:String, stationType:String, stationLineType: String, stationLatitude: String, stationLongitude: String)]?
let content = String(contentsOfURL: contentsOfURL, encoding: encoding, error: error)
stations = []
let lines:[String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]
for line in lines {
var values:[String] = []
if line != "" {
// For a line with double quotes
// we use NSScanner to perform the parsing
if line.rangeOfString("\"") != nil {
var textToScan:String = line
var value:NSString?
var textScanner:NSScanner = NSScanner(string: textToScan)
while textScanner.string != "" {
if (textScanner.string as NSString).substringToIndex(1) == "\"" {
textScanner.scanLocation += 1
textScanner.scanUpToString("\"", intoString: &value)
textScanner.scanLocation += 1
} else {
textScanner.scanUpToString(delimiter, intoString: &value)
}
// Store the value into the values array
values.append(value as! String)
// Retrieve the unscanned remainder of the string
if textScanner.scanLocation < textScanner.string.characters.count {
textToScan = (textScanner.string as NSString).substringFromIndex(textScanner.scanLocation + 1)
} else {
textToScan = ""
}
textScanner = NSScanner(string: textToScan)
}
// For a line without double quotes, we can simply separate the string
// by using the delimiter (e.g. comma)
} else {
values = line.componentsSeparatedByString(delimiter)
}
// Put the values into the tuple and add it to the items array
let station = (stationName: values[0], stationType: values[1], stationLineType: values[2], stationLatitude: values[3], stationLongitude: values[4])
stations?.append(station)
}
}
return stations
}
this is my sample .csv file
Rithala,Underground,Yellow Line,28.7209,77.1070
But i am getting an error on this line
let station = (stationName: values[0], stationType: values[1], stationLineType: values[2], stationLatitude: values[3], stationLongitude: values[4])
stations?.append(station)
Fatal error : Array index out of range
What am i doing wrong ? Please help me.
解决方案
You are attempting to parse the file path rather than the contents of the file
If you replace
let content = String(contentsOfURL: contentsOfURL, encoding: encoding, error: error)
with:
if let data = NSData(contentsOfURL: contentsOfURL) {
if let content = NSString(data: data, encoding: NSUTF8StringEncoding) {
//existing code
}
}
then the code will work for your example file.
这篇关于在Swift中解析CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文