arrays - 修改解析的 CSV 文件输出,很快吗?

标签 arrays swift csv parsing

我的 Xcode 项目中有以下文本文件:

Section Designation,Mass per metre (kg/m),Depth of section h (mm),Width of section b (mm),Web thickness tw (mm),Flange thickness tf (mm),Root radius r (mm),Depth between fillets d (mm)
1016x305x584,584.0,1056.0,314.0,36.0,64.0,30.0,868.1
1016x305x494,494.0,1036.0,309.0,31.0,54.0,30.0,868.1
1016x305x438,438.0,1026.0,305.0,26.9,49.0,30.0,868.1
1016x305x415,415.0,1020.0,304.0,26.0,46.0,30.0,868.1
1016x305x393,392.7,1015.9,303.0,24.4,43.9,30.0,868.1
1016x305x350,350.0,1008.0,302.0,21.1,40.0,30.0,868.1
1016x305x314,314.3,999.9,300.0,19.1,35.9,30.0,868.1
1016x305x272,272.3,990.1,300.0,16.5,31.0,30.0,868.1
1016x305x249,248.7,980.1,300.0,16.5,26.0,30.0,868.1
1016x305x222,222.0,970.3,300.0,16.0,21.1,30.0,868.1

我在 ViewController 中编写了以下代码,以便解析数据并将它们放入可用的数组中,我打算稍后使用它来填充我的 UITable:

class UniversalBeamsViewController: UIViewController {

    var  data:[[String:String]] = []

    var  columnTitles:[String] = []

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

       convertCSV(file: "data")

  }

    func readDataFromFile(file:String) -> String!{

        guard let filepath = Bundle.main.path(forResource: file, ofType: "txt")

            else {

                return nil

        }

        do {

            let contents = try String(contentsOfFile: filepath)

            return contents

        } catch {

            print("File Read Error for file \(filepath)")

            return nil

        }

    }

    func cleanRows(file: String)->String{

        var cleanFile = readDataFromFile(file: file)

        cleanFile = cleanFile!.replacingOccurrences(of: "\r", with: "\n")

        cleanFile = cleanFile!.replacingOccurrences(of: "\n\n", with: "\n")

        return cleanFile!

    }

    func getStringFieldsForRow(row:String, delimiter:String)-> [String]{

        return row.components(separatedBy: delimiter)

    }

    func convertCSV(file:String){

        let rows = cleanRows(file: file).components(separatedBy: "\n")

        if rows.count > 0 {

            data = []

            columnTitles = getStringFieldsForRow(row: rows.first!,delimiter:",")

            for row in rows {

                let fields = getStringFieldsForRow(row: row,delimiter: ",")

                if fields.count != columnTitles.count {continue}

                var dataRow = [String:String]()

                for (index,field) in fields.enumerated(){

                    let fieldName = columnTitles[index]

                    dataRow[fieldName] = field

                }

                data += [dataRow]

            }

        } else {

            print("No data in file")

        }

        print(data)

    }

}

但是,当执行 ConvertCSV 方法时,我得到以下数组输出:

[["Web thickness tw (mm)": "Web thickness tw (mm)", "Depth of section h (mm)": "Depth of section h (mm)", "Flange thickness tf (mm)": "Flange thickness tf (mm)", "Section Designation": "Section Designation", "Mass per metre (kg/m)": "Mass per metre (kg/m)", "Width of section b (mm)": "Width of section b (mm)", "Depth between fillets d (mm)": "Depth between fillets d (mm)", "Root radius r (mm)": "Root radius r (mm)"], ["Web thickness tw (mm)": "36.0", "Depth of section h (mm)": "1056.0", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1", "Width of section b (mm)": "314.0", "Mass per metre (kg/m)": "584.0", "Section Designation": "1016x305x584", "Flange thickness tf (mm)": "64.0"], ["Depth between fillets d (mm)": "868.1", "Flange thickness tf (mm)": "54.0", "Web thickness tw (mm)": "31.0", "Mass per metre (kg/m)": "494.0", "Section Designation": "1016x305x494", "Depth of section h (mm)": "1036.0", "Root radius r (mm)": "30.0", "Width of section b (mm)": "309.0"], ["Section Designation": "1016x305x438", "Depth between fillets d (mm)": "868.1", "Width of section b (mm)": "305.0", "Web thickness tw (mm)": "26.9", "Root radius r (mm)": "30.0", "Flange thickness tf (mm)": "49.0", "Depth of section h (mm)": "1026.0", "Mass per metre (kg/m)": "438.0"], ["Depth of section h (mm)": "1020.0", "Section Designation": "1016x305x415", "Web thickness tw (mm)": "26.0", "Mass per metre (kg/m)": "415.0", "Width of section b (mm)": "304.0", "Flange thickness tf (mm)": "46.0", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1"], ["Flange thickness tf (mm)": "43.9", "Root radius r (mm)": "30.0", "Mass per metre (kg/m)": "392.7", "Width of section b (mm)": "303.0", "Depth of section h (mm)": "1015.9", "Web thickness tw (mm)": "24.4", "Depth between fillets d (mm)": "868.1", "Section Designation": "1016x305x393"], ["Web thickness tw (mm)": "21.1", "Depth of section h (mm)": "1008.0", "Depth between fillets d (mm)": "868.1", "Mass per metre (kg/m)": "350.0", "Root radius r (mm)": "30.0", "Flange thickness tf (mm)": "40.0", "Section Designation": "1016x305x350", "Width of section b (mm)": "302.0"], ["Depth of section h (mm)": "999.9", "Width of section b (mm)": "300.0", "Web thickness tw (mm)": "19.1", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1", "Flange thickness tf (mm)": "35.9", "Section Designation": "1016x305x314", "Mass per metre (kg/m)": "314.3"], ["Web thickness tw (mm)": "16.5", "Mass per metre (kg/m)": "272.3", "Section Designation": "1016x305x272", "Depth of section h (mm)": "990.1", "Width of section b (mm)": "300.0", "Flange thickness tf (mm)": "31.0", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1"], ["Mass per metre (kg/m)": "248.7", "Root radius r (mm)": "30.0", "Depth of section h (mm)": "980.1", "Flange thickness tf (mm)": "26.0", "Web thickness tw (mm)": "16.5", "Depth between fillets d (mm)": "868.1", "Width of section b (mm)": "300.0", "Section Designation": "1016x305x249"], ["Depth between fillets d (mm)": "868.1", "Mass per metre (kg/m)": "222.0", "Flange thickness tf (mm)": "21.1", "Root radius r (mm)": "30.0", "Width of section b (mm)": "300.0", "Web thickness tw (mm)": "16.0", "Depth of section h (mm)": "970.3", "Section Designation": "1016x305x222"]]

从上面的输出中可以看出,列标题打印在数组内,并占据大主数组内的整个第一个数组。我不希望将列标题打印为我想要获得的大数组的一部分。知道我需要修改代码的哪一部分才能实现这一点吗?

问候, 沙迪。

最佳答案

你的循环

for row in rows 

将包含所有行,包括带有列名的第一行,解决此问题的一种方法是迭代索引,但从 1 而不是 0 开始

for i in 1..<rows.count {
    let row = rows[i]
    // same as before...

关于arrays - 修改解析的 CSV 文件输出,很快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092569/

相关文章:

swift - 我无法将当前位置添加到 Google map

python - 如何使用 python 将 .csv 文件中的行数据提取到单独的 .txt 文件中?

python - 如何通过 python 将我的 xlsx 文件批量转换为 CSV

java - 如何从java中的二维按钮数组中随机选择一个按钮?

javascript - 在对象中搜索关联数据

ios - UICollectionView didSelectRowAt 从未被调用

swift - 指示tableView的indexPath何时为最后一个

python - 我将如何编写一个用我的 sqlite3 数据库填充的 CSV 文件?

c++ - 初始化字符数组有问题

sql - LIMIT 和包含在单个 JSONB 查询中