如何逐行解析CSV并解析出多个关键字及其数据? [英] How can I parse CSV line by line and parse out multiple keywords and their data?

查看:129
本文介绍了如何逐行解析CSV并解析出多个关键字及其数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,其中包含以下数据(但还有更多):

I have a CSV file that contains data like the following below (but lots more):

Date             dd/mm/yyyy
ExpirationDate   dd/mm/yyyy
Lot              6760786776 
Serial           34659FSFHS45

DataType       Unknown   Count          
A(Loc1, Loc2)  Unknown   Variable1 Variable2 Variable3 
B(Loc3, Loc4)  Unknown   Variable4 Variable5 Variable6

DataType       Unknown   Apple         
A(Loc1, Loc2)  Unknown   Variable1 Variable2 Variable3 
B(Loc3, Loc4)  Unknown   Variable4 Variable5 Variable6

等...

目前,我有这样的内容:

Currently, I have something like this:

 public void DeserialCSVStream(string filePath)
    {
        using (StreamReader sr = new StreamReader(filePath))

        {
            string currentline;
            while ((currentline = sr.ReadLine()) != null)
            {
                if (currentline.IndexOf("Date", StringComparison.CurrentCultureIgnoreCase) >=0)
                {
                    Console.WriteLine(currentline);
                }
                else if (currentline.IndexOf("Lot", StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    Console.WriteLine(currentline);
                }
                else if (currentline.IndexOf("Serial", StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    Console.WriteLine(currentline);
                }
                else if (currentline.IndexOf("Count", StringComparison.CurrentCultureIgnoreCase) >= 0)
                {
                    Console.WriteLine(currentline);
                }
            }

        }

    }

哪个还可以,但给我一些问题:

Which is okay but gives me a few problems:

-如果我查找字符串"Date",它不仅给我Date,而且给我Expiration Date,但我只想解析Date.如果我使用StartsWith,它将为空.

-If I look for a string of "Date", it gives me not only Date but Expiration Date but I only want to parse out the Date. If I use StartsWith, it gives me null.

-此外,以上内容仅允许我获取字段旁边的列数据.例如. Count只返回DataType和Unknown,但我想抓住count下的整个表",而不仅仅是获取Count所在的那一行.我怎么做?

-Also, the above only lets me grab the columns data next to the field. E.g. Count only returns DataType and Unknown but I want to grab the whole "table" under count and not just that line where Count is on. How do I do that?

推荐答案

您可以使用Dictionary<string, string>,键是术语,值是...值.然后,可以将String.StartsWithStringcComparison.CurrentCultureIgnoreCase一起使用,以检查该行是否以该术语开头.您可以使用SubstringIndexOf之类的字符串方法来获取值.我假设您正在寻找空格后的值:

You could use a Dictionary<string, string>, the key is the term and the value is... the value. Then you can use String.StartsWith with StringcComparison.CurrentCultureIgnoreCase to check whether or not the line starts with that term. You get the value by using string methods like Substring or IndexOf. I assume that you're looking for the value after the space:

var lines = File.ReadLines(filePath);
var tokenValues = new Dictionary<string,string>{ { "Date", null }, { "Lot", null }, { "Serial", null } };
foreach (string line in lines)
{ 
    string l = line.TrimStart();
    string startsWithToken = tokenValues.Keys
        .FirstOrDefault(t => l.TrimStart().StartsWith(t, StringComparison.CurrentCultureIgnoreCase));
    if(startsWithToken != null)
        tokenValues[startsWithToken] = l.Substring(l.IndexOf(' ') + 1).Trim();
}

这篇关于如何逐行解析CSV并解析出多个关键字及其数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆