.net c#如何一次一行读取CSV文件并解析关键字

How to read a CSV file one line at a time and parse out keywords
2021-09-15
  •  译文(汉语)
  •  原文(英语)

我是C#的新手,并且已经开始使用StreamReader.我试图一次读取一行文件,并在与诸如"I / RPTGEN"之类的特定关键字匹配时输出该行.

到目前为止,我已经弄清楚了如何将整个文件读取为一个字符串,但是我很难弄清楚如何一次只读取一行.

到目前为止,我的代码是这样.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
class Test
{
    public static void Main()
    {
        try
        {
            using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
            {
                String line = sr.ReadToEnd();
                Console.WriteLine(line);

                Console.ReadLine();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The File could not be read:");
            Console.WriteLine(e.Message);

            Console.ReadLine();
        }
    }
}
}

另外,这里是文件中一行的示例.

咨询,2013年2月27日12:00:44,I / RPTGEN(cadinterface),I / RPTGEN失败-错误500-内部服务器错误-针对报告请求返回(检查URL日志).

速聊1:
他的问题在标题"如何一次读取一行CSV文件并解析出关键字"中.别那么苛刻.
解决过程1

如果您的CSV文件仅包含一行,则ReadToEnd可以接受,但是如果您的日志文件包含多于一行,则最好逐行读取ReadLineStreamReader对象

using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
{
    string currentLine;
    // currentLine will be null when the StreamReader reaches the end of file
    while((currentLine = sr.ReadLine()) != null)
    {
       // Search, case insensitive, if the currentLine contains the searched keyword
       if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0)
       {
            Console.WriteLine(currentLine);
       }
    }
}
速聊1:
谢谢大家的回答.它使我可以更接近完成我的项目.
速聊2:
如果我正在寻找多个关键字(例如日期和序列号),又该怎么办?如果在我的CSV中有多个包含日期的关键字(例如ExpirationDate与仅日期),该如何控制要查找的关键字?太感谢了!
速聊3:
抱歉,但是使用当前信息我无法回答.在您指定所有要求的新问题上呢?除了我以外,还会有更多人关注您的问题.当然,您可以添加一个指向该问题的链接,并在此处通过另一个指向新问题的链接通知我.
速聊4:
好的,谢谢.希望这有助于更好地阐明问题:stackoverflow.com/questions/23704320/…
速聊5:
如果CSV在实际列中有换行符(用引号引起来),则此方法将不起作用
解决过程2

一次读取一行的另一种方法是:

var searchItem = "Error 500";

var lines = File.ReadLines("c:/temp/ESMDLOG.csv");

foreach (string line in lines)
{
    if (line.Contains(searchItem))
    {
        Console.WriteLine(line);
    }
}

I am new to C# and I have started using StreamReader. I am trying to read a file one line at a time and output the line when it matches a specific keyword like "I/RPTGEN".

So far I figured out how to read the entire file into a string, but I'm having trouble figuring out how to just read it one line at a time.

My code so far is this.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
class Test
{
    public static void Main()
    {
        try
        {
            using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
            {
                String line = sr.ReadToEnd();
                Console.WriteLine(line);

                Console.ReadLine();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The File could not be read:");
            Console.WriteLine(e.Message);

            Console.ReadLine();
        }
    }
}
}

Plus here is a sample of one line in the file.

Advisory,2/27/2013 12:00:44 AM,I/RPTGEN (cadinterface),I/RPTGEN Failed - Error 500 - Internal Server Error - returned for a report request (check log for URL).

Talk1:
His question was in the title "How to read a CSV file one line at a time and parse out keywords". Don't be so harsh.
Solutions1

If your CSV file contains just one line the ReadToEnd could be acceptable, but if you have a log file composed of more than one line then it is better to read line by line using ReadLine of the StreamReader object

using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv"))
{
    string currentLine;
    // currentLine will be null when the StreamReader reaches the end of file
    while((currentLine = sr.ReadLine()) != null)
    {
       // Search, case insensitive, if the currentLine contains the searched keyword
       if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0)
       {
            Console.WriteLine(currentLine);
       }
    }
}
Talk1:
THank you all for the answers. It has allowed me to gt closer to finishing my project.
Talk2:
What if I was looking for multiple keywords like Date and Serial Number and also, what if in my CSV there are multiple keywords that contain Date e.g. ExpirationDate vs. just Date, how do I control which keywords to look for? Thank you so much!
Talk3:
Sorry, but with the current info I cannot answer. What about a new question where you specify all the requirements?. More people than just me will look at your problem. Of course you could add a link back to this question and, notify me with another link to the new question here.
Talk4:
Okay sure, thank you. Hope this helps clarify things better: stackoverflow.com/questions/23704320/…
Talk5:
This will not work if the CSV has linefeeds inside the actual columns (Encapsulated with quotes)
Solutions2

Another way to read one line at a time is:

var searchItem = "Error 500";

var lines = File.ReadLines("c:/temp/ESMDLOG.csv");

foreach (string line in lines)
{
    if (line.Contains(searchItem))
    {
        Console.WriteLine(line);
    }
}
转载于:https://stackoverflow.com/questions/15560011/how-to-read-a-csv-file-one-line-at-a-time-and-parse-out-keywords

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入