从日期时间为索引的文件中提取文本
我有大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在您已经编辑了问题以表明分隔符实际上是逗号而不是斜杠(这会与日期格式冲突),这变得容易多了。我已将昨晚的答案重新发布在下面。
显然,您可以通过添加错误处理、使用 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.
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.
因此,800 个大小约为 100KB 的文件总计为 80 KB。那么为什么不建立一个像这样的小类呢?
你还应该注意实现
GetHashCode()
和Equals()
(书中有一个很好的解释基本 C#)。你应该将接口IComparable
添加到该类中,它只会产生类似的内容如果你得到了这个,你可以轻松地执行以下操作:
所以缺少的是读取所有文件(而不是单个文件) 。但这可以通过
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
Also you should take care about implementing
GetHashCode()
andEquals()
(there is a good explanation in the book Essential C#). And you should add the interfaceIComparable
to that class which just makes somethine likeIf you got this you can easily do the following:
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 aDirectory.GetDirectories()
.After reading all the files into your List you can do whatever LINQ query comes to your mind.