Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

c# - How can I parse CSV line by line and parse out multiple keywords and their data?

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

etc....

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:

-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.

-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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That looks like a text file with a custom format, rather than a CSV (Comma Separated Value) file.

You can modify your code slightly to fix the specific problem you address by using StartsWith rather than IndexOf

if (currentline.StartsWith("Date:", StringComparison.CurrentCultureIgnoreCase))

If there is a possibility of any leading space you can change currentLine to currentLine.TrimStart().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...