2 回答

TA貢獻1810條經驗 獲得超4個贊
Cinchoo ETL-一個開源庫,可使用幾行代碼輕松地將CSV轉換為DataTable
using (var p = new ChoCSVReader("sample.csv").WithFirstLineHeader())
{
var dt = p.AsDataTable();
}
查閱CodeProject文章以獲取其他幫助。

TA貢獻1946條經驗 獲得超4個贊
您可以CsvDataReader從此存儲庫使用 https://github.com/ttustonic/LightGBMSharp
有一個CsvDataReader在實例目錄,這是一個獨立的文件,所以你不需要休息。
它實現了一個IDataReader接口,可用于加載DataTable。
這是一個例子。假設您的csv文件如下所示:
Id Name Age
1 Mark 100
2 Joe 32
3 Betty 55
該代碼:
var dt = new DataTable();
using (var rdr = new CsvDataReader(file, true)) // set true if the csv has header line, false otherwise
{
//rdr.ColumnTypes = new Type[] { typeof(int), typeof(string), typeof(int) }; Uncomment this if you know the structure of the csv
dt.Load(rdr);
}
foreach (DataRow r in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
Console.Write($"{dt.Columns[i]}:{r[i]} ");
Console.WriteLine("");
}
將給出以下輸出:
Id:1 Name:Mark Age:100
Id:2 Name:Joe Age:32
Id:3 Name:Betty Age:55
默認分隔符是TAB,可以在構造函數中更改。
- 2 回答
- 0 關注
- 212 瀏覽
添加回答
舉報