使用C#解析文本文件 [英] Parsing Text Files Using C#
问题描述
我想从文本文件中提取数据
我有以下格式的文本文件 -
2002/01/10 00:44:51。 53 40 。 4415 -126。 0167 25 。 37 3 。 92 Md 56 269 147 0 。 29 NCSN 21208454
i使用以下代码从文本文件中获取数据
public 类数据
{
public string 日期,时间;
public double lat,lon,depth,mag;
}
class 计划
{
static void Main( string [] args)
{
string dt;
列表< data> gd = new List< data>();
使用(StreamReader sr = new StreamReader( E:\\op.html))
{
while (sr.Peek()> 0 )
{
string str;
string [] arr;
str = sr.ReadLine();
arr = str.Split(' ');
data d = new data();
d.date = arr [ 0 ];
// d.time = arr [1];
// d.lat = Convert.ToDouble(arr [2]);
// d.lon = Convert.ToDouble(arr [3]);
// d.depth = Convert.ToDouble(arr [4]);
// d.mag = Convert.ToDouble(arr [5]);
Console.WriteLine (d.date);
// Console.WriteLine(d.time);
// Console.WriteLine(d.lat);
// Console.WriteLine(d.lon);
// < span class =code-comment> Console.WriteLine(d.depth);
// Console.WriteLine(d.mag);
Console.ReadKey();
}
在上面的代码我得到的价值仅为d.date,但标有//的行会引发错误Index out of bounds ...&我不能得到剩下的价值。
i想要d.time,d.lat,d.lon,d.depth,d.mag ......
的值我怎么做到这一点??
好吧,这个想法基本上是正确的。当然,这种解析是僵硬和脆弱的,因为格式的一点点改变会破坏功能。当然,它可以改进,但只是稍微改善。
您需要的修复是:
使用(StreamReader sr = new StreamReader( TestFile.txt)){
string 线;
while ((line = sr.ReadLine())!= null ){
// 在拆分
string arr [] = line.Split( new char [] {' '},System.StringSplitOptions.RemoveEmptyEntries);
// ...
} // loop
} // 使用;这里是自动调用sr.Dispose的地方
注意:System.StringSplitOptions.RemoveEmptyEntries
将增加一些稳定性,以防你有多个空格或其他复杂功能(你可以组合几个分隔符,使第一个参数成为一个字符数组)。
你没有其他领域,因为... 5下一行被注释掉了。 :-)
再次添加它们,据我所知,它们应该可以工作。在调试器下运行它。即使是最小的调试技巧也可以帮助你立刻解决问题。
祝你好运,
-SA
i want to extract data from a text file
I have a text file in the following format-
2002/01/10 00:44:51.53 40.4415 -126.0167 25.37 3.92 Md 56 269 147 0.29 NCSN 21208454
i have used the following code to get the data from the text file
public class data
{
public string date, time;
public double lat, lon,depth,mag;
}
class Program
{
static void Main(string[] args)
{
string dt;
List<data> gd = new List<data>();
using (StreamReader sr = new StreamReader("E:\\op.html"))
{
while (sr.Peek() > 0)
{
string str;
string[] arr;
str = sr.ReadLine();
arr = str.Split(' ');
data d = new data();
d.date = arr[0];
//d.time = arr[1];
//d.lat = Convert.ToDouble( arr[2]);
//d.lon = Convert.ToDouble(arr[3]);
//d.depth = Convert.ToDouble(arr[4]);
//d.mag = Convert.ToDouble(arr[5]);
Console.WriteLine(d.date);
//Console.WriteLine(d.time);
//Console.WriteLine(d.lat);
//Console.WriteLine(d.lon);
//Console.WriteLine(d.depth);
//Console.WriteLine(d.mag);
Console.ReadKey();
}
in the above code i get the value only of d.date, but the lines marked with "//" throws an error "Index out of bounds..." & i cant get the rest of the values.
i want the values for d.time,d.lat,d.lon,d.depth,d.mag..
how do i achieve this ??
Well, the idea is basically correct. Of course, such parsing is rigid and fragile, as a little change in format would break the functionality. Of course, it can be improved, but only slightly.
The fix you need is this:
using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; while ((line = sr.ReadLine()) != null) { // remove all lines from your previous code inside loop before Split string arr[] = line.Split(new char[] {' '}, System.StringSplitOptions.RemoveEmptyEntries); // ... } //loop } // using; here is where sr.Dispose is automatically called
Pay attention:System.StringSplitOptions.RemoveEmptyEntries
will add some stability, in case you have more than one blank space or other complication (you can combine several separators making a first parameter a character array).
And you don't other fields because… 5 next lines are commented out. :-)
Add them again, as far as I can see, they should work. Run it all under debugger. Even the minimal debugging skills will help you to get it right in no time.
Good luck,
—SA
这篇关于使用C#解析文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!