将从表中提取的纯文本转换为对象

时间:2018-01-03 10:05:38

标签: c# text-parsing

您好我想弄清楚最好的algorythm解析纯文本到对象这里是解析表的例子:

Tilte Code quantity price sum
Item1 item1 2 1.50 3.00
Item2 item2 1 3.00 3.00
good
Item3 item3 2 1.00 2.00

对象结果应如下所示:

title = Item1, code = item1, quantity = 2, price = 1.50, sum = 3.00

title = Item2 good, code = item2, quantity = 1, price = 3.00, sum = 3.00

title = Item3, code = item3, quantity = 2, price = 1.00, sum = 2.00

等等。主要问题是标题或代码包含换行符。如此简单的逐行阅读是行不通的。你遇到过同样的问题吗?也许你之前做了一些algorythm并解决了这个问题。请分享你的想法。等待答复。

1 个答案:

答案 0 :(得分:0)

如果您的数据摘录代表了一个黄金标准,那么这应该可以解决问题:

// String[] data = File.ReadAllLines(@"C:\MyFile.txt");

String[] data = 
{ 
    "Tilte Code quantity price sum",
    "Item1 item1 2 1.50 3.00",
    "Item2 item2 1 3.00 3.00",
    "good",
    "Item3 item3 2 1.00 2.00"
};

CultureInfo ci = new CultureInfo("en-US");
List<dynamic> objects = new List<dynamic>();
String title = String.Empty;

foreach (String line in data.Skip(1))
{
    String[] parts = line.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

    if (parts.Length == 1)
    {
        title = line;
        continue;
    }

    String titleItem = parts[0];

    if (title != String.Empty)
        titleItem += " " + title;

    objects.Add(new
    {
        Title = titleItem,
        Code = parts[1],
        Quantity = Int32.Parse(parts[2]),
        Price = Single.Parse(parts[3], ci),
        Sum = Single.Parse(parts[4], ci),
    });
}

foreach (dynamic obj in objects)
{
    Console.WriteLine("[ENTRY]");
    Console.WriteLine(obj.Title);
    Console.WriteLine(obj.Code);       
    Console.WriteLine(obj.Quantity);
    Console.WriteLine(obj.Price);
    Console.WriteLine(obj.Sum);
    Console.WriteLine();
}

输出:

[ENTRY]
Item1
item1
2
1,5
3

[ENTRY]
Item2
item2
1
3
3

[ENTRY]
Item3 good
item3
2
1
2

您可以尝试访问this link的实时演示。