arrays - 快速修改解析的 CSV 文件输出?
问题描述
我的 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...
推荐阅读
- c# - 是什么导致 ASP.Net Core 中的 DataReader 出现 mySQL 命令并发问题,我该如何解决?
- java - 使用 ID 从另一个表中选择项目
- typescript - 在Vue中使用箭头函数无法获取gata
- laravel - Gitlab 在运行 PHP 单元后遇到致命错误但测试通过
- python - 试图将鼠标坐标发送到arduino但在python中冻结
- python - 比较两个列表
- python - Tensorflow 使用 AI 创建图像
- javascript - 尽管使用草皮倒带,TopoJSON 仍绘制实心矩形
- python-3.x - Incremental learning in keras
- node.js - 给图片的一部分着色最简单的方法是什么