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">
但输入文件可能会因列数发生变化(因此我无法定义抽象类)
当然,我可以只写正则表达式来逐行分解输入文件,但我很感兴趣知道是否有其他人已经完成了它(或者它是一个标准的库函数)。
谢谢。