C#:解析文本文件 [英] C# : Parse a Text File

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

问题描述

大家好,

我对使用C#进行编程完全不熟悉,还有如何解析.txt文件的问题吗?

我的.txt文件看起来像这样

价值29.500000 
ID abcde
Matrix1
1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000
Point1 0.000000 0.000000 0.000000
Point2 1.000000 0.000000 0.000000
Point3 0.000000 1.000000 0.000000
Point1Dim -1.000000
Point2Dim -1.000000
Size1 30.000000
Size2 20.000000
Size3 15.000000




等等。我的目标是提取Point1,Point2等等。

非常感谢帮助和指导。

谢谢

Rinkie


[edit]已添加代码块 - OriginalGriff [/ edit]

解决方案

您需要的第一件事是 StreamReader [ ^ ]读取文件,一次一行, ReadLine [ ^ ]或一气呵成使用 ReadToEnd [ ^ ]。然后,您可以使用 String <的方法/ a> [ ^ ]课程查找特定行并将文本拆分为单独的字段。最后使用 Double.Parse [ ^ ]将字符串转换为 Double 值的方法。


当涉及到这样的东西时,没有真正的捷径。 />
首先,它似乎按行排列:首先将其转换为行集合。

 字符串 [] lines = File.ReadAllLines(路径); 



那么,我可能会为每种类型设置一个List< string>想要提取:

 List< string> Points =  new  List< string>(); 
List< string> PointDim = new List< string>();
List< string> Size = new List< string>();
... < / string > ; < / string > < / string > < / string > < span class =code-keyword>< / string > < / 字符串 >
最后,Id开始查看集合,并将它们转换为内部类(或内部类实例的集合) )


 IEnumerable< string> allLines; 
string filePath = @ C:\\ \\data\data.txt;
if (File.Exists(filePath))
{
// 读取文件的所有内容并将其存储到使用新行拆分的列表中
allLines = File.ReadLines(filePath);
}

// 所有第1行
IEnumerable< string> point1Lines = allLines.Where(d = > d.StartsWith( Point1,StringComparison.CurrentCultureIgnoreCase));

foreach string line in point1Lines)
{
string [] values = line.Split(' '); // 空格或选项卡或其他文件包含分隔符
for int i = < span class =code-digit> 1
; i< words; > {
string value = values [i]; // 跳过索引0,它包含标签,剩下的是点数据
// 逻辑到这里
}
} < / 字符串 > < / string >


Hi All,
I'm completely new to programming in C# and have this question of how to go about parsing a .txt file?
My .txt file looks something like this

Value	29.500000
ID      abcde
Matrix1
1.000000	0.000000	0.000000	0.000000	
0.000000	1.000000	0.000000	0.000000	
0.000000	0.000000	1.000000	0.000000	
0.000000	0.000000	0.000000	1.000000	
Point1 	0.000000	0.000000	0.000000
Point2	1.000000	0.000000	0.000000
Point3	0.000000	1.000000	0.000000
Point1Dim	-1.000000
Point2Dim	-1.000000
Size1	30.000000
Size2	20.000000
Size3	15.000000
:
:
:


and so on. And my goal is to extract Point1, Point2 and so on.
Help and guidance would be greatly appreciated.
Thanks
Rinkie

[edit]Code block added - OriginalGriff[/edit]

解决方案

The first thing you will need is a StreamReader[^] to read the file, either one line at a time with ReadLine[^] or all in one go with ReadToEnd[^]. You can then use methods of the String[^] class to find specific lines and split the text into separate fields. Finally use the Double.Parse[^] method to convert character strings to Double values.


There are no real short cuts when it comes to stuff like this.
Firstly, it seems to be organised in lines: so start by converting it to a collection of lines.

string[] lines = File.ReadAllLines(path);


Then, I would probably set up a List<string>for each type you want to extract:

List<string> Points = new List<string>();
List<string> PointDim = new List<string>();
List<string> Size = new List<string>();
...</string></string></string></string></string></string>

Then I'd start looping through the lines separating out the lines into the appropriate collections.
Finally, Id start looking at the collections, and converting them into internal classes (or collections of internal class instances)


IEnumerable<string> allLines; 
string filePath = @"C:\data\data.txt";
 if (File.Exists(filePath))
 {
  //Read all content of the files and store it to the list split with new line 
   allLines = File.ReadLines(filePath);
 }

 //all Point 1 lines
 IEnumerable<string> point1Lines = allLines.Where(d => d.StartsWith("Point1", StringComparison.CurrentCultureIgnoreCase));

foreach(string line in point1Lines)
{
  string[] values= line.Split(' ');//either space or tab or others as your file contain seperator  
  for(int i=1; i <words;>  {
      string value = values[i];// skip index 0,it contains label, remaining are point data
     //logic goes here
  }
}</string></string>


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

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