gpt4 book ai didi

c# - 在不知道结构的情况下将 CSV 读入数据表

转载 作者:行者123 更新时间:2023-12-02 00:33:19 26 4
gpt4 key购买 nike

我正在尝试将 CSV 读入数据表。

CSV 可能有数百列,但最多只有 20 行。

它看起来像这样:

+----------+-----------------+-------------+---------+---+
| email1 | email2 | email3 | email4 | … |
+----------+-----------------+-------------+---------+---+
| ccemail1 | anotherccemail1 | 3rdccemail1 | ccemail | |
| ccemail2 | anotherccemail2 | 3rdccemail2 | | |
| ccemail3 | anotherccemail3 | | | |
| ccemail4 | anotherccemail4 | | | |
| ccemail5 | | | | |
| ccemail6 | | | | |
| ccemail7 | | | | |
| … | | | | |
+----------+-----------------+-------------+---------+---+

我正在尝试使用genericparser为了这;但是,我相信它需要您知道列名称。

string strID, strName, strStatus;
using (GenericParser parser = new GenericParser())
{
parser.SetDataSource("MyData.txt");

parser.ColumnDelimiter = "\t".ToCharArray();
parser.FirstRowHasHeader = true;
parser.SkipStartingDataRows = 10;
parser.MaxBufferSize = 4096;
parser.MaxRows = 500;
parser.TextQualifier = '\"';

while (parser.Read())
{
strID = parser["ID"]; //as you can see this requires you to know the column names
strName = parser["Name"];
strStatus = parser["Status"];

// Your code here ...
}
}

有没有办法在不知道列名的情况下将此文件读入数据表?

最佳答案

就这么简单!

        var adapter = new GenericParsing.GenericParserAdapter(filepath);
DataTable dt = adapter.GetDataTable();

这将自动为您完成所有操作。

关于c# - 在不知道结构的情况下将 CSV 读入数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11569777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com