C# 在不知道结构的情况下将CSV读入数据表
我正在尝试将CSV读入数据表 CSV可能有数百列,最多只有20行 它看起来像这样:C# 在不知道结构的情况下将CSV读入数据表,c#,.net,csv,datatable,C#,.net,Csv,Datatable,我正在尝试将CSV读入数据表 CSV可能有数百列,最多只有20行 它看起来像这样: +----------+-----------------+-------------+---------+---+ | email1 | email2 | email3 | email4 | … | +----------+-----------------+-------------+---------+---+ | ccemail1 | anotherccemail1 |
+----------+-----------------+-------------+---------+---+
| email1 | email2 | email3 | email4 | … |
+----------+-----------------+-------------+---------+---+
| ccemail1 | anotherccemail1 | 3rdccemail1 | ccemail | |
| ccemail2 | anotherccemail2 | 3rdccemail2 | | |
| ccemail3 | anotherccemail3 | | | |
| ccemail4 | anotherccemail4 | | | |
| ccemail5 | | | | |
| ccemail6 | | | | |
| ccemail7 | | | | |
| … | | | | |
+----------+-----------------+-------------+---------+---+
我正试着用这个;但是,我认为这需要您知道列名
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 firstColumn = parser[0]
将0替换为列号。
列的数量可以使用
parser.ColumnCount
我不熟悉
GenericParser
,我建议使用,或这样的工具
但这种简单的手动方法也应该有效:
IEnumerable<String> lines = File.ReadAllLines(filePath);
String header = lines.First();
var headers = header.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
DataTable tbl = new DataTable();
for (int i = 0; i < headers.Length; i++)
{
tbl.Columns.Add(headers[i]);
}
var data = lines.Skip(1);
foreach(var line in data)
{
var fields = line.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
DataRow newRow = tbl.Rows.Add();
newRow.ItemArray = fields;
}
IEnumerable lines=File.ReadAllLines(filePath);
字符串头=lines.First();
var headers=header.Split(新[]{',},StringSplitOptions.RemoveEmptyEntries);
DataTable tbl=新的DataTable();
for(int i=0;i
太简单了
var adapter = new GenericParsing.GenericParserAdapter(filepath);
DataTable dt = adapter.GetDataTable();
这将自动为您完成所有操作。我使用通用解析器来完成。 在第一次运行循环时,我获取列名称,然后引用它们以将它们添加到列表中 在我的例子中,我已经对数据进行了透视,但是这里有一个代码示例,如果它对某人有帮助的话
bool firstRow = true;
List<string> columnNames = new List<string>();
List<Tuple<string, string, string>> results = new List<Tuple<string, string, string>>();
while (parser.Read())
{
if (firstRow)
{
for (int i = 0; i < parser.ColumnCount; i++)
{
if (parser.GetColumnName(i).Contains("FY"))
{
columnNames.Add(parser.GetColumnName(i));
Console.Log("Column found: {0}", parser.GetColumnName(i));
}
}
firstRow = false;
}
foreach (var col in columnNames)
{
double actualCost = 0;
bool hasValueParsed = Double.TryParse(parser[col], out actualCost);
csvData.Add(new ProjectCost
{
ProjectItem = parser["ProjectItem"],
ActualCosts = actualCost,
ColumnName = col
});
}
}
bool firstRow=true;
List columnNames=新列表();
列表结果=新列表();
while(parser.Read())
{
如果(第一行)
{
for(int i=0;i
CSV表示逗号分隔吗?是否总是有标题…
意味着可能会有更多的列,而您不知道有多少列,或者该列是空的?@TimSchmelter是的,始终会有一列header@Blam你能详细说明一下吗?我如何知道何时停止[0][1]…[243]。。?我事先不知道有多少列。有parser.columns之类的吗?非常感谢。布拉姆在上面也提出了同样的建议,但我事先不知道有多少列。我会说,一个通用解析器正在重新发明轮子。有许多预滚CSV解析器可以在不知道列名的情况下工作。如果您定义了数据透视,或者提供了数据透视后实际输入数据的外观示例,则可能会对经验不足的用户有所帮助