如何解析segju,jk
How to parsegju,jk
我必须解析具有以下结构的 TSV 文件:
[教派
“1 2”
“2 3”他的?
我不确定这是否是您想要做的。我还注意到记录之间似乎有 4 个空格。如果真的是制表符,可以把分隔符改成制表符Delimiter = "\t",
void Main()
{
var data = @"[Section one of info]
""Atr1 1""
""Atr2 2""
[Section two of info]
""Atr3 Atr4""
""1 2""
""2 3""
""4 5""";
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = " ",
Mode = CsvMode.Escape
};
using (var reader = new StringReader(data))
using (var csv = new CsvReader(reader, config))
{
var isSectionOne = true;
var record = new Foo() { Atr3 = new List<int>(), Atr4 = new List<int>() };
while(csv.Read())
{
if (csv.GetField(0).StartsWith("["))
continue;
if (isSectionOne)
{
if (csv.GetField(0) == "Atr1")
{
record.Atr1 = csv.GetField<int>(1);
}
if (csv.GetField(0) == "Atr2")
{
record.Atr2 = csv.GetField<int>(1);
}
if (csv.GetField(0) == "Atr3")
{
isSectionOne = false;
}
}
else
{
record.Atr3.Add(csv.GetField<int>(0));
record.Atr4.Add(csv.GetField<int>(1));
}
}
record.Dump();
}
}
public class Foo
{
public int Atr1 { get; set; }
public int Atr2 { get; set; }
public List<int> Atr3 { get; set; }
public List<int> Atr4 { get; set; }
}
我必须解析具有以下结构的 TSV 文件: [教派 “1 2” “2 3”他的?
我不确定这是否是您想要做的。我还注意到记录之间似乎有 4 个空格。如果真的是制表符,可以把分隔符改成制表符Delimiter = "\t",
void Main()
{
var data = @"[Section one of info]
""Atr1 1""
""Atr2 2""
[Section two of info]
""Atr3 Atr4""
""1 2""
""2 3""
""4 5""";
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = " ",
Mode = CsvMode.Escape
};
using (var reader = new StringReader(data))
using (var csv = new CsvReader(reader, config))
{
var isSectionOne = true;
var record = new Foo() { Atr3 = new List<int>(), Atr4 = new List<int>() };
while(csv.Read())
{
if (csv.GetField(0).StartsWith("["))
continue;
if (isSectionOne)
{
if (csv.GetField(0) == "Atr1")
{
record.Atr1 = csv.GetField<int>(1);
}
if (csv.GetField(0) == "Atr2")
{
record.Atr2 = csv.GetField<int>(1);
}
if (csv.GetField(0) == "Atr3")
{
isSectionOne = false;
}
}
else
{
record.Atr3.Add(csv.GetField<int>(0));
record.Atr4.Add(csv.GetField<int>(1));
}
}
record.Dump();
}
}
public class Foo
{
public int Atr1 { get; set; }
public int Atr2 { get; set; }
public List<int> Atr3 { get; set; }
public List<int> Atr4 { get; set; }
}