【问题标题】:How can I create an array of dictionary from a txt file in Swift?如何在 Swift 中从 txt 文件创建字典数组?
【发布时间】:2015-03-02 04:14:41
【问题描述】:

这是txt文件的示例。

route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color

53739,,11,11TH AVENUE,,3,,3333CC,FFFFFF

53740,,17,1700 南,,3,,3333CC,FFFFFF 53741,,2,200 南,,3,,3333CC,FFFFFF

第一行显示元素的名称,每个组件用逗号分隔。

我想创建一个字典数组,如下所示:

[[“route_id”:“53739”,“agency_id”:“”,“route_short_name”:“17”......]

["route_id":"53740", "agency_id":""...... ]]

我猜你明白了。

【问题讨论】:

    标签: arrays parsing swift dictionary


    【解决方案1】:

    来源:http://pjeremymalouf.com/scan-a-csv-into-swift/

    import Foundation
    
    class CSVScanner {
    
        class func arrayOfDictionaryFromFile(#columnNames:Array<String>, fromFile theFileName:String, withFunction theFunction:(Dictionary<String, String>)->()) {
    
            if let strBundle = NSBundle.mainBundle().pathForResource(theFileName, ofType: "csv") {
    
                var encodingError:NSError? = nil
    
                if let fileObject = NSString(contentsOfFile: strBundle, encoding: NSUTF8StringEncoding, error: &encodingError){
    
                    var fileObjectCleaned = fileObject.stringByReplacingOccurrencesOfString("\r", withString: "\n")
    
                    fileObjectCleaned = fileObjectCleaned.stringByReplacingOccurrencesOfString("\n\n", withString: "\n")
    
                    let objectArray = fileObjectCleaned.componentsSeparatedByString("\n")
    
                    for anObjectRow in objectArray {
    
                        let objectColumns = anObjectRow.componentsSeparatedByString(",")
    
                        var aDictionaryEntry = Dictionary<String, String>()
    
                        var columnIndex = 0
    
                        for anObjectColumn in objectColumns {
    
                            aDictionaryEntry[columnNames[columnIndex]] = anObjectColumn.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
    
                            columnIndex++
                        }
    
                        if (aDictionaryEntry.count > 1) {
                            theFunction(aDictionaryEntry)
                        }
                    }
                }
            }
        }
    }
    

    如何使用它:

    var myCSVContents = Array<Dictionary<String, String>>()
    
    CSVScanner.runFunctionOnRowsFromFile(["title", "body", "category"], withFileName: "fileName.csv", withFunction: {
    
        (aRow:Dictionary<String, String>) in
    
        myCSVContents.append(aRow)
    
    })
    

    确保将文件保存为 .csv 或修改代码以查找 .txt

    【讨论】:

    • 天哪,这太完美了!
    • 我的声望不够
    【解决方案2】:

    我建议您使用 JSON 格式制作文本文件并对其进行解析。我认为这将是您更简单的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 2021-05-10
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多