从日期时间为索引的文件中提取文本

发布于 2024-09-04 01:04:35 字数 1760 浏览 2 评论 0原文

我有大约 800 个文件,每个文件最大 55KB-100KB,其中数据采用以下格式:

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

Date 为 DD/MM/YYYY 格式,Time 为 HH 格式: MM

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

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

我正在尝试绕过,形成一个来执行搜索和提取操作。我不知道该怎么做,想知道一些想法。

我已经编写了下面的代码:

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

帮助赞赏。 谢谢 索汉姆

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

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

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.

I have written the code below:

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


    }

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

Help appreciated.
Thanks
Soham

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-09-11 01:04:35

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

// 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 中操作字符串。

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

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.

蛮可爱 2024-09-11 01:04:35

因此,800 个大小约为 100KB 的文件总计为 80 KB。那么为什么不建立一个像这样的小类呢?

public class Entry
{
    public DateTime Date {get; set;}
    public float Float1 {get; set;}
    public int Integer1 {get; set;}

    public Entry(string values)
    {
        //ToDo: Parse single line into properties
        //      e.g. use String.Split, RegEx, etc.
    }
}

你还应该注意实现 GetHashCode()Equals() (书中有一个很好的解释基本 C#)。你应该将接口 IComparable 添加到该类中,它只会产生类似的内容

public int CompareTo(Entry rhs)
{
    return this.Date.CompareTo(rhs.Date);
}

如果你得到了这个,你可以轻松地执行以下操作:

var allEntries = new SortedList<Entry>();

string currentLine = null;

using (var streamReader = new StreamReader("C:\\MyFile.txt"))
    while ((currentLine = streamReader.ReadLine()) != null)
    {
        try
        {
            var entry = new Entry(currentLine);
            allEntries.Add(entry);
        }
        catch (Exception ex)
        {
            //Do whatever you like
            //maybe just
            continue;
            //or
            throw;
        }
    }

所以缺少的是读取所有文件(而不是单个文件) 。但这可以通过 Directory.GetFiles() 上的另一个循环来完成,该循环本身可能是通过 Directory.GetDirectories() 循环的。

将所有文件读入列表后,您可以执行您想到的任何 LINQ 查询。

So 800 files with around 100KB sums up to 80 KBytes. So why don't built up a little class like

public class Entry
{
    public DateTime Date {get; set;}
    public float Float1 {get; set;}
    public int Integer1 {get; set;}

    public Entry(string values)
    {
        //ToDo: Parse single line into properties
        //      e.g. use String.Split, RegEx, etc.
    }
}

Also you should take care about implementing GetHashCode() and Equals() (there is a good explanation in the book Essential C#). And you should add the interface IComparable to that class which just makes somethine like

public int CompareTo(Entry rhs)
{
    return this.Date.CompareTo(rhs.Date);
}

If you got this you can easily do the following:

var allEntries = new SortedList<Entry>();

string currentLine = null;

using (var streamReader = new StreamReader("C:\\MyFile.txt"))
    while ((currentLine = streamReader.ReadLine()) != null)
    {
        try
        {
            var entry = new Entry(currentLine);
            allEntries.Add(entry);
        }
        catch (Exception ex)
        {
            //Do whatever you like
            //maybe just
            continue;
            //or
            throw;
        }
    }

So what's missing is to read in all the files (instead of a single one). But this can be done by another loop on Directory.GetFiles() which maybe itself is looped through a Directory.GetDirectories().

After reading all the files into your List you can do whatever LINQ query comes to your mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文