2
我有興趣讀一個CSV文件和輸出列表<列表<字符串>>
let readCsv (filepath:string) : string list list =
//.......................
input file:
Quote1,Quote2,Quote3
"Hello,World","He said:""Yes""",Example
Output:
// Type: string list list
[["Quote1";"Quote2";"Quote3"];
["Hello,World"; "He said:"Yes"";"Example"]]
Input2:
1,2,3,4,5,6
7,8,9,10,11,12
Output2:
// Type: string list list
[["1";"2";"3";"4";"5";"6"];
["7";"8";"9";"10";"11";"12"]]
然而,一些的NuGet包,例如CsvHelper,FileHelper,F#數據依賴於定義一個類來「捕獲」數據,或者通過引用一個csv文件來定義一個類型。
https://joshclose.github.io/CsvHelper/
http://www.filehelpers.net/example/QuickStart/ReadWriteRecordByRecord/
http://fsharp.github.io/FSharp.Data/index.html
例如:
// In C#, from FileHelper Documentation
[DelimitedRecord(",")]
public class AbstractClass
{
public string Quote1;
public string Quote2;
public string Quote3;
}
或
// F# Data Documentation
type AbstractType = CsvProvider<"../example.csv">
但輸入文件可能會因列數發生變化(因此我無法定義抽象類)
當然,我可以只寫正則表達式來逐行分解輸入文件,但我很感興趣知道是否有其他人已經完成了它(或者它是一個標準的庫函數)。
謝謝。