如何在C#中使用分隔符将文本文件拆分为二维数组?

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

Vlan20 is up,line protocol is up 
  Hardware is EtherSVI,address is 588d.0939.ffb4 (bia 588d.0939.ffb4)
  Description: MATS Network
  Internet address is 10.88.5.49/28
  MTU 1500 bytes,BW 100000 Kbit/sec,DLY 100 usec,reliability 255/255,txload 1/255,rxload 1/255
  Encapsulation ARPA,loopback not set
  Keepalive not supported 
  ARP type: ARPA,ARP Timeout 04:00:00
  Last input 00:00:04,output never,output hang never
  Last clearing of "show interface" counters never
  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 1
  Queueing strategy: fifo
  Output queue: 0/40 (size/max)
  5 minute input rate 0 bits/sec,0 packets/sec
  5 minute output rate 0 bits/sec,0 packets/sec
     1992293 packets input,187299894 bytes,0 no buffer
     Received 22809 broadcasts (0 IP multicasts)
     0 runts,0 giants,0 throttles 
     0 input errors,0 CRC,0 frame,0 overrun,0 ignored
     2115535 packets output,813500880 bytes,0 underruns
     0 output errors,1 interface resets
     89225 unknown protocol drops
     0 output buffer failures,0 output buffers swapped out

文本文件中的下半部分是小数点,这就是为什么我尝试使用double

1/1/2018;0;29;10
1/2/2018;0;16;1
1/3/2018;0;32;1
1/4/2018;0;34;15
1/5/2018;0;19;2
1/6/2018;0;21;2

我试图拆分分号,并将分号之间的每个值分配到一个包含31行4列的2D数组中。

1/29/2018;0.32;52;38
1/30/2018;0.06;44;21

我相信我具有将值插入2d数组所需的正确循环,但是,我在输入时遇到了一个错误,并且我相信这是由于斜线引起的。

我的代码足以将文本文件的内容保存到数组中吗?以及我该如何处理private void button1_Click(object sender,EventArgs e) { // 2d array double[,] testarray = new double[31,4]; string inputFile = File.ReadAllText("testywesty.txt"); char[] spearator = {';',' '}; for (int row = 0; row < 31; row++) { for (int column = 0; column < 4; column++) { string[] strlist = inputFile.Split(spearator); testarray [row,column] = double.Parse(strlist[column]); } } } 个字符?

may0777 回答:如何在C#中使用分隔符将文本文件拆分为二维数组?

您将在斜杠上得到一个错误,因为您试图将其转换为双精度,这是不可能的。您可以做的一件事是,首先使用该类的DateTime方法将它们转换为Parse,然后使用ToOADate方法将其转换为double。请注意,如果需要将其转换回去,则可以像在下面的输出中一样使用DateTime.FromOADate方法。

此外,使用File.ReadAllLines将文件读入字符串数组可能会有所帮助,其中每个字符串都是一个文件行(假设每一行都包含您所需要的四个部分,在问题中的示例文件内容中)。这样,每行代表一个row,然后我们可以拆分该行以获得columns

例如:

private static void button1_Click(object sender,EventArgs e)
{
    var lines = File.ReadAllLines("testywesty.txt");
    var items = new double[lines.Length,4];
    var delims = new[] {';',' '};

    for (var line = 0; line < lines.Length; line++)
    {
        var parts = lines[line].Split(delims);
        var maxParts = Math.Min(parts.Length,items.GetLength(1));

        for (var part = 0; part < maxParts; part++)
        {
            if (part == 0)
            {
                // Parse first item to a date then use ToOADate to make it a double
                items[line,part] = DateTime.Parse(parts[part]).ToOADate();
            }
            else
            {
                items[line,part] = double.Parse(parts[part]);
            }
        }
    }

    // Show the output
    var output = new StringBuilder();
    for (var row = 0; row < items.GetLength(0); row++)
    {
        var result = new List<string>();
        for (var col = 0; col < items.GetLength(1); col++)
        {
            result.Add(col == 0
                ? DateTime.FromOADate(items[row,col]).ToShortDateString()
                : items[row,col].ToString());
        }

        output.AppendLine(string.Join(",",result));
    }
    MessageBox.Show(output.ToString(),"Results");
}

输出

enter image description here

,

当然,您可以读取数据并将其解析为数组。但是,由于数组是多态的,因此必须为object[,]类型。这就是我的处理方式:

class Program
{

    static void Main(string[] args)
    {
        object[,] array = ReadFileAsArray("testywesty.txt");            
    }

    static object[,] ReadFileAsArray(string file)
    {
        // how long is the file?
        // read it twice,once to count the rows 
        // and a second time to read each row in
        int rows = 0;

        var fs = File.OpenText(file);
        while (!fs.EndOfStream)
        {
            fs.ReadLine();
            rows++;
        }
        fs.Close();

        var array = new object[rows,4];
        fs = File.OpenText(file);
        int row = 0;
        while (!fs.EndOfStream)
        {
            // read line
            var line = fs.ReadLine();
            // split line into string parts at every ';'
            var parts = line.Split(';');
            // if 1st part is date store in 1st column
            if (DateTime.TryParse(parts[0],out DateTime date))
            {
                array[row,0] = date;
            }
            // if 2nd part is flaot store in 2nd column
            if (float.TryParse(parts[1],out float x))
            {
                array[row,1] = x;
            }
            // if 3rd part is integer store in 3rd column
            if (int.TryParse(parts[2],out int a))
            {
                array[row,2] = a;
            }
            // if 4rd part is integer store in 4rd column
            if (int.TryParse(parts[3],out int b))
            {
                array[row,3] = b;
            }
            row++;
        }
        fs.Close();

        return array;
    }
}

但是我觉得这很笨拙。如果文件代表的数据类型是预先确定的,而不是填充自定义类型的集合,则在C#中感觉更自然,因为您可以让该类型处理其自己的数据并进行解析。考虑下面的示例:

class Program
{

    static void Main(string[] args)
    {
        IEnumerable<MyData> list = ReadFileAsEnumerable("testywesty.txt");

        Debug.WriteLine(MyData.ToHeading());
        foreach (var item in list)
        {
            Debug.WriteLine(item);
        }            
        // date        x    a    b   
        // 1/1/2018       0   29   10
        // 1/2/2018       0   16    1
        // 1/3/2018       0   32    1
        // 1/4/2018       0   34   15
        // 1/5/2018       0   19    2
        // 1/6/2018       0   21    2
        // 1/29/2018   0.32   52   38
        // 1/30/2018   0.06   44   21
    }

    public static IEnumerable<MyData> ReadFileAsEnumerable(string file)
    {
        var fs = File.OpenText(file);
        while (!fs.EndOfStream)
        {
            yield return MyData.Parse(fs.ReadLine());
        }
        fs.Close();
    }
}

/// <summary>
/// Stores a row of my data
/// </summary>
/// <remarks>
/// Mutable structures are evil. Make all properties read-only.
/// </remarks>
public struct MyData
{
    public MyData(DateTime date,float number,int a,int b)
    {
        this.Date = date;
        this.Number= number;
        this.A=a;
        this.B=b;
    }
    public DateTime Date { get; }
    public float Number { get; }
    public int A { get; }
    public int B { get; }

    public static MyData Parse(string line)
    {
        // split line into string parts at every ';'
        var parts = line.Split(';');
        // if 1st part is date store in 1st column
        if (DateTime.TryParse(parts[0],out DateTime date)) { }
        // if 2nd part is flaot store in 2nd column
        if (float.TryParse(parts[1],out float number)) { }
        // if 3rd part is integer store in 3rd column
        if (int.TryParse(parts[2],out int a)) { }
        // if 4rd part is integer store in 4rd column
        if (int.TryParse(parts[3],out int b)) { }

        return new MyData(
            date,number,a,b);
    }
    public static string ToHeading()
    {
        return $"{"date",-11} {"x",-4} {"a",-4} {"b",-4}";
    }
    public override string ToString()
    {
        return $"{Date.ToShortDateString(),-11} {Number,4} {A,4} {B,4}";
    }
}
本文链接:https://www.f2er.com/3047397.html

大家都在问