有条件地解析txt文件并将输出写入文件 [英] Parse a txt file conditionally and write output to a file

查看:78
本文介绍了有条件地解析txt文件并将输出写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将txt文件解析为所需的o / p,以便在完成解析后我可以将其作为CSV打开。通过对Linq的一点了解,我尝试围绕解析过程进行编码,但我遗漏了一些我自己无法解决的问题。任何帮助表示赞赏。



输入文件:

 Left Rulegd ATTR001.PDST.MPDTMP01(A001PMTR )PROTY(01),BRPKY(A0Y)
A001PMTR 0 00AF 123456A11 2011/12/14 ENT tmp
Left Rulegd ATTR001.PDST.MPDTMP01(B001PRTY)PROTY(01),BRPKY(A0Y)
B001PRTY 1 0 12AB 7891P12 2010/08/25 AMP tmp
B234561 2 0 0 7891P12 2011/09/12 URTST tmp
Left Rulegd ATTR001.PDST.MPDTMP01(C001AMEF)PROTY(01),BRPKY (A0Y)
C001AMEF 0 PZ89 123456A11 2013/11/02 AMP tmp
Left Rulegd ATTR001.PDST.MPDTMP01(D001AAM)PROTY(01),BRPKY(A0Y)
D001AAM 0 OP25 123456A11 2009 / 02/14 ENT tmp





期望输出:

 ATTR001.PDST.MPDTMP01(A001PMTR),PROTY(01),BRPKY(A0Y),123456A11,2011 / 12/14,ENT tmp 
ATTR001.PDST.MPDTMP01(B001PRTY),PROTY(01),BRPKY (A0Y),7891P12,2010 / 08/25,AMP tmp
ATTR001.PDST.MPDTMP01(C001AMEF),PROTY(01),BRPKY(A0Y),123456A11,2013 / 11/02,AMP tmp
ATTR001.PDST.MPDTMP01(D001AAM),PROTY(01),BRPKY( A0Y),123456A11,2009 / 02/14,ENT tmp





我的尝试:



  void  Main()
{
< span class =code-keyword> int counter = 0 ;
string 行;
string line1;
string srchBegin = Left Rulegd< /跨度>;
char delim = ' ';
IEnumerable< string> allRecords;

string filePath = @ C:\Users\vgruber\Desktop\input.TXT;
if (File.Exists(filePath))
{
allRecords = File.ReadLines(filePath);
}
else
{
Console.WriteLine( 文件不存在!);
}

StreamReader reader = File.OpenText(filePath);
尝试
{

((行= reader.ReadLine())!= null
{

if (line.Contains(srchBegin))
{

var values = line.Split( new char [] {' '},StringSplitOptions.RemoveEmptyEntries);
var result = String .Join( ,values).Remove( 0 12 );
Console.WriteLine(result);
}
其他
{
var values1 = line.Substring( 30 );
var result1 = string .Join( ,values1);
Console.WriteLine(result1);
}
}
reader.Close();
}
catch (例外)
{

}
}

// 在此定义其他方法和类

解决方案

取决于具体情况的许多可能性:

- 如果要删除的文本在行首时始终相同。只需跳过前23个字符。

这样的东西甚至可以解决问题。

  var  result = line.SubString( 23 ); 





- 如果要删除的部分更多是模式,RegEx(正则表达式)更有可能提供帮助。



[更新]

格式化输入后,事情看起来有点复杂,RegEx肯定是有序的。

我会构建一个RegEx来识别有趣的部分和使用replace来重构结果。

开始解决方案的示例:

  string  pattern =  @  ^ * Left Rulegd *([^] +)*([^] +)\ n([^] +) ; 
string input = Left Rulegd ATTR001。 PDST.MPDTMP01(A001PMTR)PROTY(01),BRPKY(A0Y)\ nAA001PMTR 0 00AF 123456A11 2011/12/14 ENT tmp;
string replacement =


1,

2,

I am trying to parse a txt file into a desired o/p so that I can open it as a CSV once parse is completed. With a little knowledge of Linq, I tried to code around parse process but I am missing something that I myself unable to figure it out. Any help is appreciated.!

Input file:

          Left Rulegd  ATTR001.PDST.MPDTMP01(A001PMTR)  PROTY(01),BRPKY(A0Y)
A001PMTR           0    00AF   123456A11  2011/12/14  ENT tmp
          Left Rulegd  ATTR001.PDST.MPDTMP01(B001PRTY)  PROTY(01),BRPKY(A0Y)
B001PRTY   1       0    12AB   7891P12    2010/08/25  AMP tmp
B234561    2       0       0   7891P12    2011/09/12  URTST tmp
          Left Rulegd  ATTR001.PDST.MPDTMP01(C001AMEF)  PROTY(01),BRPKY(A0Y)
C001AMEF           0    PZ89   123456A11  2013/11/02  AMP tmp
          Left Rulegd  ATTR001.PDST.MPDTMP01(D001AAM)  PROTY(01),BRPKY(A0Y)
D001AAM            0    OP25   123456A11  2009/02/14  ENT tmp



Desired output:

ATTR001.PDST.MPDTMP01(A001PMTR),PROTY(01),BRPKY(A0Y),123456A11,2011/12/14,ENT tmp 
ATTR001.PDST.MPDTMP01(B001PRTY),PROTY(01),BRPKY(A0Y),7891P12,2010/08/25,AMP tmp 
ATTR001.PDST.MPDTMP01(C001AMEF),PROTY(01),BRPKY(A0Y),123456A11,2013/11/02,AMP tmp 
ATTR001.PDST.MPDTMP01(D001AAM),PROTY(01),BRPKY(A0Y),123456A11,2009/02/14,ENT tmp



What I have tried:

void Main()
{
	int counter = 0;
	string line;
	string line1;
	string srchBegin = "Left Rulegd";
	char delim = ' ';
	IEnumerable<string> allRecords;

	string filePath = @"C:\Users\vgruber\Desktop\input.TXT";
	if (File.Exists(filePath))
	{
		allRecords = File.ReadLines(filePath);
	}
	else
	{
		Console.WriteLine("File do not exist!");
	}

	StreamReader reader = File.OpenText(filePath);
	try
	{
	
	while ((line = reader.ReadLine()) != null)
	{
		
		if (line.Contains(srchBegin))
		{
			
			var values = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			var result = String.Join(",", values).Remove(0, 12);			
			Console.WriteLine(result);	
		}
		else
		{
			var values1 = line.Substring(30);
			var result1 = string.Join(",", values1);
			Console.WriteLine(result1);			
		}
	}
	reader.Close();
	}
	catch(Exception er) 
	{
		
	}
}

// Define other methods and classes here

解决方案

Many possibilities depending on situation:
- if the text to remove is always the same at beginning of line. Just skip the first 23 chars.
Something like this can even do the trick.

var result = line.SubString(23);



- if the part to remove is more of a pattern, RegEx (Regular Expressions) are more likely to help.

[Update]
After formatting the input, it appear that things are a little more complicated and RegEx are definitely on order.
I would build a RegEx to identify interesting parts with groups and use replace to recompose the result.
Example with beginning of solution:

string pattern =  @"^ *Left Rulegd *([^ ]+) *([^ ]+)\n([^ ]+)";
string input = "          Left Rulegd  ATTR001.PDST.MPDTMP01(A001PMTR)  PROTY(01),BRPKY(A0Y)\nA001PMTR           0    00AF   123456A11  2011/12/14  ENT tmp";
string replacement = "


1,


2,


这篇关于有条件地解析txt文件并将输出写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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