使用特定条件解析文本文件 [英] Parsing a Text file using a particular condition

查看:60
本文介绍了使用特定条件解析文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是解析文本文件中的文字。

<前一行=文字> 10:06:59:卖出订单59266 5,000 TL在22.20排队账户的常规账本市场#CC1233
10:06:59:票据47930卖单59165266 5,000 TGL常规市场满22.20 - 剩余0对于账户#CC1233
10:31:57:买单59172 1,000 GHI在21.90排队在正常账户定期市场账户#CC1233
10:32:11:买入订单59127 2,000 PIC在25.31排队在常规账本定期市场账户#CC1233
10: 32:47:机票47922买入订单59173072 1,000 GHNI常规市场满21.90 - 剩余0对于账户#CC1233
10:35:29:买入订单59135 1,000 NYP在38.25排队在常规账本定期市场账户# CC1233
10:59:17:买入订单59056 2,000 MYF,18.35在常规账本常规市场排队等账号#CC1233
10:59:17:票据47959买入订单59180956 2,000 MLCF常规市场在18.35 - 剩余0对于账户#CC1322
10:59:36:买入订单59127 2,000 PYC为25.35更改为常规账户常规市场账户#CC1233
11:04:49:买入订单59707 5,000 BYL在14.70排队进入正常账户常规市场账户#CC1233
11:08:18:买入订单59635 1,000 NYP在38.50更改为常规账户常规市场账户#CC1233
11:08:18 :买入订单54035 1,000 NYP在38.50排队在常规账本常规市场账号#CC1233
11:08:18:票47318买单59174035 500 NYP常规市场满38.50 - 剩余500账户#CC1233
11:08:18:机票47959买入订单59174035 500 NYP常规市场满38.50 - 剩余0对于账户#CC1233



此文件通过另一个自动持续更新软件。我想用那个单词Ticket解析那些行,其中有一些其他行与其他文本。



分隔所选行(带有单词的单词) )使用空间并将其保存在数据库中。记住文件不断更新。



甚至可能..?

解决方案

Of当然有可能。

在每次(定期)''解析和插入操作''你必须:

  1. 检查当前文件大小是否为大于一定数量的最后操作文件大小(如果小或相等则中止操作)。
  2. 仅解析输入文件的扩充部分,在数据库中插入所有行包含单词'' Ticket ''(请查看 String.Contains [ ^ ]方法)。
  3. 使用当前文件更新'last operation''文件大小。


 U必须读取文件并将其插入一个atatable 

int counter = 0 ;
string 行;

DataTable table = new DataTable();
DataColumn cl = new DataColumn( Text );
table.Columns.Add(cl);
// 读取文件并逐行显示。
系统。 IO.StreamReader文件=
new System.IO.StreamReader( 文件路径/ test.txt的);
while ((line = file.ReadLine())!= null
{

DataRow dr = table.NewRow();
Console.WriteLine(line);
dr [ Text] = line;
table.Rows.Add(dr);
counter ++;
}

file.Close();



// 暂停屏幕。
Console.ReadLine();

将数据插入数据表应用表排序使用 选择

u可以参考这个示例

DataTable table = new DataTable( Players);
table.Columns.Add( new DataColumn( 大小 typeof int )));
table.Columns.Add( new DataColumn( 性别 typeof char )));

table.Rows.Add( 100 ' F');
table.Rows.Add( 235 ' F');
table.Rows.Add( 250 ' M');
table.Rows.Add( 310 ' M');
table.Rows.Add( 150 ' M');

// 搜索超过一定规模的人。
// ...需要某种性别。
DataRow [] result = table.Select( Size> = 230 AND Sex ='m');
foreach (DataRow row in result)
{
Console。 WriteLine( {0},{1},row [ 0 ],行[ 1 ]); // 您可以在此处插入数组
}


What i am trying to do is to parse below text from a text file.

10:06:59:Sell Order 59266 5,000 TL at 22.20 Queued in the Regular Book Regular Market  for Account # CC1233
10:06:59: Ticket 47930 Sold Order 59165266 5,000 TGL Regular Market filled at 22.20  - remaining 0 For Account # CC1233
10:31:57:Buy Order 59172 1,000 GHI at 21.90 Queued in the Regular Book Regular Market  for Account # CC1233
10:32:11:Buy Order 59127 2,000 PIC at 25.31 Queued in the Regular Book Regular Market  for Account # CC1233
10:32:47: Ticket 47922 Bought Order 59173072 1,000 GHNI Regular Market filled at 21.90  - remaining 0 For Account # CC1233
10:35:29:Buy Order 59135 1,000 NYP at 38.25 Queued in the Regular Book Regular Market  for Account # CC1233
10:59:17:Buy Order 59056 2,000 MYF at 18.35 Queued in the Regular Book Regular Market  for Account # CC1233
10:59:17: Ticket 47959 Bought Order 59180956 2,000 MLCF Regular Market filled at 18.35  - remaining 0 For Account # CC1322
10:59:36: Buy Order 59127  2,000 PYC at 25.35 Changed in the Regular Book Regular Market  for Account # CC1233
11:04:49:Buy Order 59707 5,000 BYL at 14.70 Queued in the Regular Book Regular Market  for Account # CC1233
11:08:18: Buy Order 59635  1,000 NYP at 38.50 Changed in the Regular Book Regular Market  for Account # CC1233
11:08:18:Buy Order 54035 1,000 NYP at 38.50 Queued in the Regular Book Regular Market  for Account # CC1233
11:08:18: Ticket 47318 Bought Order 59174035 500 NYP Regular Market filled at 38.50  - remaining 500 For Account # CC1233
11:08:18: Ticket 47959 Bought Order 59174035 500 NYP Regular Market filled at 38.50  - remaining 0 For Account # CC1233


This file is continuously updated automatically via another software. I want to parse on those line with a word "Ticket" in it there are some other lines with other text.

Separate the selected line (the one with ticket word) using space and saving them in database. keeping in mind the file is updated continuously.

is it even possible..?

解决方案

Of course it is possible.
At each (periodic) ''parse and insert operation'' you have to:

  1. Check if current file size is bigger than a certain amount with respect ''last operation'' file size (abort operation if it is smaller or equal).
  2. Parse only the augmented part of the input file, inserting in the database all the lines containing the word ''Ticket'' (have a look at String.Contains[^] method).
  3. Update ''last operation'' file size with current one.


    U have to read file and insert it into one datatable
    
                int counter = 0;
                string line;
    
                DataTable table = new DataTable();
                DataColumn cl = new DataColumn("Text");
                table.Columns.Add(cl);
                // Read the file and display it line by line.
                System.IO.StreamReader file =
                   new System.IO.StreamReader("Filepath/test.txt");
                while ((line = file.ReadLine()) != null)
                {
    
                    DataRow dr = table.NewRow();
                    Console.WriteLine(line);
                    dr["Text"] = line;
                    table.Rows.Add(dr);
                    counter++;
                }
    
                file.Close();
    
    
    
                // Suspend the screen.
                Console.ReadLine();
    
    After inserting data into datatable apply table sorting using select
    
    u can refer this example
    
    DataTable table = new DataTable("Players");
    	table.Columns.Add(new DataColumn("Size", typeof(int)));
    	table.Columns.Add(new DataColumn("Sex", typeof(char)));
    
    	table.Rows.Add(100, 'f');
    	table.Rows.Add(235, 'f');
    	table.Rows.Add(250, 'm');
    	table.Rows.Add(310, 'm');
    	table.Rows.Add(150, 'm');
    
    	// Search for people above a certain size.
    	// ... Require certain sex.
    	DataRow[] result = table.Select("Size >= 230 AND Sex = 'm'");
    	foreach (DataRow row in result)
    	{
    	    Console.WriteLine("{0}, {1}", row[0], row[1]); // Here you can insert those rows in Array
    	}


    这篇关于使用特定条件解析文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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