简体   繁体   中英

how to remove spaces and identify multiple spaces and place them in a table data from text file?

i have huge text tables in a text file and i am able to read data inside my winform application but there is some irregular data coming inside the datagrid. i just needed the table as it is, into my datagrid. i want to catch "*( 3)" from the data and using that number i want to split the data below. The Plate Id column can have n number of Id's. So according to that Plate Id's X-coord and Y-coord's will be added to them. how to place this table data inside a datagrid without disturbing it's structure? I need to re-use this data from datagrid again.

*( 3)CAR PLATE COORDINATES:
*  No.   Plate  Plate   No.   X-Coord   Y-Coord
* Plate  Type    Id    Coord    (in)      (in)
    2    'CA'     1      5     8.6250  -23.3750
                              32.6249  -23.3750
                              46.5983  120.6250
                              46.5983  120.6250
                               8.6250  120.6250
         'CA'     2      5     8.6250  120.6250
                              46.5983  120.6250
                              64.6250  306.3959
                              59.3717  369.4359
                               8.6250  365.2070
         'CA'     3      5     8.6250  120.6250
                              46.5983  120.6250
                              64.6250  306.3959
                              59.3717  369.4359
                               8.6250  365.2070

lbl2.Text = fdlg.FileName;
                IEnumerable<String> lines = File.ReadLines(lbl2.Text);
                if (lines.Count() > 0 && lines.Contains("*( 3)"))
                {

                    foreach (var columnName in lines.FirstOrDefault().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        dataGridView3.Columns.Add(columnName, columnName);
                            while (lines.Contains(null))
                                break;


                        foreach (var cellValues in lines.Skip(1))
                        {
                            var cellArray = cellValues.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    while (lines.Contains("*("))
                        {
                    foreach (var columnName in lines.FirstOrDefault()
                        .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))                       
                            dataGridView3.Columns.Add(columnName, columnName);
                        }
                }

           }  
}

Try following code which uses fixed width columns instead of splitting columns

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



namespace ConsoleApplication49
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static int[] COLUMN_WIDTHS = { 9, 7, 7, 7, 9, 9};
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("No. Plate", typeof(int));
            dt.Columns.Add("Plate Type", typeof(string));
            dt.Columns.Add("Plate Id", typeof(int));
            dt.Columns.Add("No. Coord", typeof(int));
            dt.Columns.Add("X-Coord", typeof(double));
            dt.Columns.Add("Y-Coord", typeof(double));

            int noPlate = 0;
            string plateType = "";
            int plateId = 0;
            int noCoord = 0;

            StreamReader reader = new StreamReader(FILENAME);
            string inputLine = "";
            while ((inputLine = reader.ReadLine()) != null)
            {
                if (!inputLine.StartsWith("*"))
                {
                    string[] splitArray = FixedColumns(inputLine);

                    DataRow newRow = dt.Rows.Add();

                    if (splitArray[0].Trim().Length > 0)
                    {
                        noPlate = int.Parse(splitArray[0]);
                    }
                    newRow["No. Plate"] = noPlate;

                    if (splitArray[1].Trim().Length > 0)
                    {
                        plateType  = splitArray[1];
                    }
                    newRow["Plate Type"] = plateType;

                    if (splitArray[2].Trim().Length > 0)
                    {
                        plateId  = int.Parse(splitArray[2]);
                    }
                    newRow["Plate Id"] = plateId;

                    if (splitArray[3].Trim().Length > 0)
                    {
                        noCoord  = int.Parse(splitArray[3]);
                    }
                    newRow["No. Coord"] = noCoord;

                    newRow["X-Coord"] = double.Parse(splitArray[4]);
                    newRow["y-Coord"] = double.Parse(splitArray[5]);
                }
            }


        }
        static string[] FixedColumns(string input)
        {
            string[] splitArray = new string[COLUMN_WIDTHS.Length];
            int startCol = 0;
            for (int i = 0; i < COLUMN_WIDTHS.Length; i++)
            {
                if (i < COLUMN_WIDTHS.Length - 1)
                {
                    splitArray[i] = input.Substring(startCol, COLUMN_WIDTHS[i]).Trim();
                }
                else
                {
                    splitArray[i] = input.Substring(startCol).Trim();
                }
                startCol += COLUMN_WIDTHS[i];
            }

            return splitArray;
        }

    }

}

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