简体   繁体   中英

parsing text file to data table with irregular rows

i am trying to parse a tabular data in a text file into a data table.

the text file contains text

  PID USERNAME  THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
  11 root        1 171   52     0K    12K RUN     23:46 80.42% idle
  12 root        1 -20 -139     0K    12K RUN AS    0:56  7.96% swi7:

the code i have is like

 public class Program
{
    static void Main(string[] args)
    {
        var lines = File.ReadLines("bb.txt").ToArray();
        var headerLine = lines[0];
        var dt = new DataTable();
        var columnsArray = headerLine.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        var dataColumns = columnsArray.Select(item => new DataColumn { ColumnName = item });
        dt.Columns.AddRange(dataColumns.ToArray());
        for (int i = 1; i < lines.Length; i++)
        {
            var rowLine = lines[i];
            var rowArray = rowLine.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            var x = dt.NewRow();
            x.ItemArray = rowArray;
            dt.Rows.Add(x);

        }
    }
}

i get an error that "Input array is longer than the number of columns in this table" at second attempt on

x.ItemArray = rowArray;

Off course because second row has "RUN AS" as the value of 8th column. it also has a space between it which is a common split character for the entire row hence creating a mismatch between array's length and columns length.

what is the possible solution for this kind of situation.

Assuming that "RUN AS" is your only string that causes you the condition like this, you could just run var sanitizedLine = rowLine.Replace("RUN AS", "RUNAS") before your split and then separate the words back out afterwards. If this happens more often, however, you may need to set a condition to check that the array generated by the split matches the length of the header, then combine the offending indexes in a new array of the correct length before attempting to add it.

Ideally, however, you would instead have whatever is generating your input file wrap strings in quotes to make your life easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM