如何创建字典从斯威夫特一个txt文件中的数组? [英] How can I create an array of dictionary from a txt file in Swift?

查看:144
本文介绍了如何创建字典从斯威夫特一个txt文件中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是txt文件的样本。

This is the sample of the txt file.

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

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

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

53740,17,1700 SOUTH ,, 3,3333CC,FFFFFF
  53741,2200 SOUTH ,, 3,3333CC,FFFFFF

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

第一行显示元素的名称和各部件之间用逗号分隔。

first line shows the name of the elements and each components are separated by commas.

我要创建词典的数组看起来像这样:

I want to create an array of dictionaries that looks like this:

[ROUTE_ID:53739,agency_id:,route_short_name:17......]

[ ["route_id":"53739", "agency_id": "", "route_short_name":"17"......]

[ROUTE_ID:53740,agency_id:......]]

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

我猜你上心。

推荐答案

来源: 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或适应code查找.TXT

Make sure to save your file as .csv or adapt code to look for .txt

这篇关于如何创建字典从斯威夫特一个txt文件中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆