根据值使用TextFieldParser读取列 [英] Read Columns depending on their values using TextFieldParser
问题描述
我在C#中使用TextFieldParser读取CSV文件。我可以设法读取一个简单的CSV文件,但这个CSV文件有多种格式化风格取决于第一列名称,如下所示:
>>姓名,年龄,地点,
Joe,29,NY
Maggie,25,NJ
> ,州,人口
纽约,NY,8.49
CSV有更多类型的信息。
我试图读取列取决于其格式,但它只读列名称。如何使它只读取目标格式类型?代码的部分在这里:
while(!parser.EndOfData)
{
if(parser.ReadFields()。包含(>> Name))
{
header =
string [] fields = parser.ReadFields();
if(header)
{
foreach(var val in fields)
{
dt.Columns.Add(val);
}
header = false;
continue;
}
dt.Rows.Add(fields);
}
else {};
}
EDIT: b
>>符号是当我有列名时说的标志。
看起来好像这个文件包含多个表的数据,每个下一个表的头都以>>
开头。您可以以这种方式填充 DataSet
:
DataSet ds = new DataSet();
using(var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@C:\Temp\textfile.txt))
{
parser.Delimiters = new string [] {,};
parser.HasFieldsEnclosedInQuotes = true; //< --- !!!
string [] lineFields;
while((lineFields = parser.ReadFields())!= null)
{
if(lineFields [0] .StartsWith(>>))
{
DataTable dt = new DataTable();
DataColumn [] cols = lineFields
.Where(t =>!String.IsNullOrWhiteSpace(t))
.Select(t => new DataColumn(t.Trim '>')))
.ToArray();
dt.Columns.AddRange(cols);
ds.Tables.Add(dt);
}
else if(ds.Tables.Count> 0)
{
DataTable lastTable = ds.Tables [ds.Tables.Count - 1];
lineFields = lineFields
.Where(t =>!String.IsNullOrWhiteSpace(t))
.Take(lastTable.Columns.Count)
.Select(t => t.Trim())
.ToArray();
lastTable.Rows.Add(lineFields);
}
}
}
将生成 DataSet
与以下两个 DataTables
:
表1:
表2:
I'm reading a CSV File using TextFieldParser in C#. I could manage to read a simple CSV file but this CSV file has multiple formatting styles depending on the First Column name, like this:
>>Name, Age, Location,
"Joe","29","NY"
"Maggie", "25", "NJ"
>>City, State, Population
"New York", "NY","8.49"
THE CSV HAS MANY MORE TYPES OF INFORMATION.
I'm trying to read the columns depending on their formatting, but it only reads the column names. How can I make it read only the target formatting type? The part of the code that does that is here:
while (!parser.EndOfData)
{
if (parser.ReadFields().Contains(">>Name"))
{
header = true;
string[] fields = parser.ReadFields();
if (header)
{
foreach (var val in fields)
{
dt.Columns.Add(val);
}
header = false;
continue;
}
dt.Rows.Add(fields);
}
else {};
}
EDIT:
The ">>" symbols are a flag to say when I have column names.
It seems as if this file contains data of multiple tables, every next table's header starts with >>
. You could fill a DataSet
in this way:
DataSet ds = new DataSet();
using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:\Temp\textfile.txt"))
{
parser.Delimiters = new string[] { "," };
parser.HasFieldsEnclosedInQuotes = true; // <--- !!!
string[] lineFields;
while ((lineFields = parser.ReadFields()) != null)
{
if (lineFields[0].StartsWith(">>"))
{
DataTable dt = new DataTable();
DataColumn[] cols = lineFields
.Where(t => !String.IsNullOrWhiteSpace(t))
.Select(t => new DataColumn(t.Trim(' ', '>')))
.ToArray();
dt.Columns.AddRange(cols);
ds.Tables.Add(dt);
}
else if(ds.Tables.Count > 0)
{
DataTable lastTable = ds.Tables[ds.Tables.Count - 1];
lineFields = lineFields
.Where(t => !String.IsNullOrWhiteSpace(t))
.Take(lastTable.Columns.Count)
.Select(t => t.Trim())
.ToArray();
lastTable.Rows.Add(lineFields);
}
}
}
Tested with your sample data. A DataSet
with following two DataTables
is generated:
Table1:
Table2:
这篇关于根据值使用TextFieldParser读取列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!