从文件中提取文本,其中date -t​​ime是索引 [英] Extracting text from a file where date -time is the index

查看:152
本文介绍了从文件中提取文本,其中date -t​​ime是索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约800个文件,最多为55KB-100KB,数据为这种格式。

I have got around 800 files of maximum 55KB-100KB each where the data is in this format

日期,时间,浮点数,浮点数,浮点数,浮点数,整数

Date,Time,Float1,Float2,Float3,Float4,Integer

日期为DD / MM / YYYY格式,时间格式为HH:MM

Date is in DD/MM/YYYY format and Time is in the format of HH:MM

日期的范围从5月1日至6月1日和6日的每一天,时间从09:00到15:30不等。

Here the date ranges from say 1st May to 1June and each day, the Time varies from 09:00 to 15:30.

我想运行一个程序,每个文件,它提取与特定给定日期有关的数据并写入一个文件。

I want to run a program so that, for each file, it extracts the data pertaining to a particular given date and writes to a file.

我正在努力寻找,形成一个搜索和提取操作。我不知道怎么做,想要有一些想法。

I am trying to get around, to form a to do a search and extract operation. I dont know, how to do it, would like to have some idea.

我已经写下了代码:

static void Main(string[] args)
    {
        string destpath = Directory.GetCurrentDirectory();
        destpath += "\\DIR";
        DirectoryInfo Dest = Directory.CreateDirectory(destpath);
        DirectoryInfo Source = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\IEOD");
        FileInfo[] fiArr = Source.GetFiles("*.csv");
        Console.WriteLine("Search Date:");
        string srchdate = Console.ReadLine();
        String FileNewLine;
        String FileNewdt;
        FileInfo r;
        foreach (FileInfo f in fiArr)
        {
            r = new FileInfo(destpath + "\\" + f.Name);
            r.Create();
            StreamWriter Sw = r.AppendText();                
            StreamReader Sr = new StreamReader(f.FullName);

            while (Sr.Peek() >= 0)
            {
                FileNewLine = Sr.ReadLine();
                FileNewdt = FileNewLine.Substring(0,10);
                if (String.Compare(FileNewdt, srchdate, true) == 0)
                {
                    //write it to a file;
                    Console.WriteLine(FileNewLine);

                }
            }

        }
        Console.ReadKey();


    }

到目前为止,它应该写入控制台。在StreamWriter的帮助下写作将在以后完成,但我遇到运行时错误。它说:''C:\Documents and Settings\Soham Das\Desktop\Test\DIR\ABAN.csv',因为它被另一个进程使用。
这里ABAN是一个新创建的文件,按代码。问题出在 StreamWriter Sw = r.AppendText()

As of now, it should write into the Console. The writing with the help of StreamWriter will be done later, but I am facing a runtime error. It says, " 'C:\Documents and Settings\Soham Das\Desktop\Test\DIR\ABAN.csv' because it is being used by another process." Here ABAN is a newly created file, by the code. The problem is faced at StreamWriter Sw = r.AppendText()

帮助赞赏。
感谢
Soham

Help appreciated. Thanks Soham

推荐答案

现在您已经编辑了问题,以显示分隔符实际上是逗号而不是斜杠(这将与日期格式冲突)这变得容易得多。我已经重新发布了从昨天晚上的答案。

Now that you have edited the question to show that the delimiter is actually a comma instead of a slash (which would have conflicted with the date format) this becomes a lot easier. I've re-posted the answer from last night below.

// This would come from Stream.ReadLine() or something
string line = "02/06/2010,10:05,1.0,2.0,3.0,4.0,5";

string[] parts = line.Split(',');
DateTime date = DateTime.ParseExact(parts[0], "dd/MM/yyyy", null);
TimeSpan time = TimeSpan.Parse(parts[1]);
date = date.Add(time); // adds the time to the date
float float1 = Single.Parse(parts[2]);
float float2 = Single.Parse(parts[3]);
float float3 = Single.Parse(parts[4]);
float float4 = Single.Parse(parts[5]);
int integer = Int32.Parse(parts[6]);

Console.WriteLine("Date: {0:d}", date);
Console.WriteLine("Time: {0:t}", date);
Console.WriteLine("Float1: {0}", float1);
Console.WriteLine("Float2: {0}", float2);
Console.WriteLine("Float3: {0}", float3);
Console.WriteLine("Float4: {0}", float4);
Console.WriteLine("Integer: {0}", integer);

显然,您可以通过添加错误处理,使用TryParse等来增强弹性,但这应该给您是如何在.NET中操作字符串的基本思想。

Obviously you can make it more resilient by adding error handling, using TryParse, etc. But this should give you a basic idea of how to manipulate strings in .NET.

这篇关于从文件中提取文本,其中date -t​​ime是索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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