如何读取CSV文件一行的时间和解析出关键字 [英] How to read a CSV file one line at a time and parse out keywords

查看:161
本文介绍了如何读取CSV文件一行的时间和解析出关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#和我一直在使用的StreamReader 启动。我想读的时间和输出线文件的一行时,它像I / RPTGEN一个特定的关键字匹配。



到目前为止,我想出如何读取整个文件转换成字符串,但我无法找出如何只读取它的一行的时间。



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

 使用系统。 
:使用System.IO;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;

命名空间ConsoleApplication5
{
类测试
{
公共静态无效的主要()
{

{使用
(StreamReader的SR =新的StreamReader(C:/temp/ESMDLOG.csv))
{
串线= sr.ReadToEnd();
Console.WriteLine(线);

到Console.ReadLine();
}
}
赶上(例外五)
{
Console.WriteLine(文件无法读取:);
Console.WriteLine(e.Message);

到Console.ReadLine();
}
}
}
}



加这里是在该文件中的一行的一个样本。




咨询2 /2013分之27上午12时零零分44秒,I / RPTGEN(cadinterface),I / RPTGEN失败 - 错误500 - 内部服务器错误 - 对报告请求返回(查看日志的URL)



解决方案

如果您的CSV文件只包含一个行 ReadToEnd的是可以接受的,但如果你有一个以上的线组成的日志文件,那么最好是用来阅读逐行的ReadLine 的StreamReader 对象

 <的code>使用(StreamReader的SR =新的StreamReader(C:/temp/ESMDLOG.csv))
{
串currentLine;
//当StreamReader的文件达到
,而((currentLine = sr.ReadLine())!= NULL)
{
//搜索结束currentLine将是空的,区分大小写,如果currentLine包含所搜索的关键字
如果(currentLine.IndexOf(I / RPTGEN,StringComparison.CurrentCultureIgnoreCase)GT; = 0)
{
Console.WriteLine(currentLine) ;
}
}
}


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

解决方案

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);
       }
    }
}

这篇关于如何读取CSV文件一行的时间和解析出关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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