C#从文本文件中解析元素

时间:2016-01-20 15:36:21

标签: c# file parsing text

我有一个包含以下内容的文本文件:

139862,2 - 455452,6 - 13:24:53 

139860,8 - 455452,2 - 13:25:13 

139859,3 - 455452,2 - 13:25:33

如您所见,文本文件的每行有3个元素。我希望能够将这些保存为变量。

我可以为每个变量添加我希望能够保存的前缀,以便文本看起来像:

X:139862,2 - Y:455452,6 - D:13:24:53 

X:139860,8 - Y:455452,2 - D:13:25:13 

X:139859,3 - Y:455452,2 - D:13:25:33

我已经得到的是用于读取行的文本文件行的编码,如下所示:

    string line;
    System.IO.StreamReader file = new System.IO.StreamReader(filePath);
    while ((line = file.ReadLine()) != null)
    {
        // Here I'd like to parse the text file and add them to a list for example
    }

提前致谢!

4 个答案:

答案 0 :(得分:1)

您可以使用string.Splitfloat.ParseDateTime.Parse

string line;
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
    string[] parts = line.Split('-');
    float x = float.Parse(parts[0].Trim(), NumberFormatInfo.InvariantInfo);
    float y = float.Parse(parts[1].Trim(), NumberFormatInfo.InvariantInfo);
    DateTime d = DateTime.Parse(parts[2].Trim(), DateTimeFormatInfo.InvariantInfo);

    Console.WriteLine("X: {0}, Y: {1}, D: {2}", x, y, d);
}

请注意,如果字符串无效,则*.Parse会引发异常。因此,您可能希望使用TryParse或将其包装在try/catch块中。

我使用*FormatInfo.InvariantInfo来确保正确解析,:

如果你有这样的数据类

public class Record
{
    public float X { get; set; }
    public float Y { get; set; }
    public DateTime D { get; set; }
}

您可以通过循环生成这些记录的列表:

List<Record> records = new List<Record>();
string line;
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
    string[] parts = line.Split('-');
    records.Add(new Record {
        X = float.Parse(parts[0].Trim(), NumberFormatInfo.InvariantInfo),
        Y = float.Parse(parts[1].Trim(), NumberFormatInfo.InvariantInfo);
        D = DateTime.Parse(parts[2].Trim(), DateTimeFormatInfo.InvariantInfo)});
}

答案 1 :(得分:1)

这个问题的解决方案可以非常直接地完成:

string line;
System.IO.StreamReader file = new System.IO.StreamReader(filePath);
List<string> newlines = new List<string>(); //here to store your List of string
string[] delimiters = new string[] { " - " }; //declared only once outside of while loop
while ((line = file.ReadLine()) != null)
{
    string[] words = line.Split(delimiters);
    newlines.Add("X:" + words[0] + " - Y:" + words[1] + " - Z:" + words[2]);
}

您的最终结果将在newlines

答案 2 :(得分:0)

试试这个:

string[] stringParsed = line.split(" - ");

答案 3 :(得分:0)

我使用了StringReader但你可以用StgreamReader替换来读取文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "139862,2 - 455452,6 - 13:24:53\n" +
            "139860,8 - 455452,2 - 13:25:13\n" +
            "139859,3 - 455452,2 - 13:25:33\n";

            string inputLine = "";
            StringReader reader = new StringReader(input);
            while((inputLine = reader.ReadLine()) != null)
            {
                string[] inputArray = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Console.WriteLine("X:{0} {1} Y:{2} {3} D:{4}", inputArray[0], inputArray[1], inputArray[2], inputArray[3], inputArray[4]);
            }
            Console.ReadLine();
        }
    }
}
相关问题