[英]how to remove spaces and identify multiple spaces and place them in a table data from text file?
我在一个文本文件中有巨大的文本表,我能够在Winform应用程序中读取数据,但是在datagrid中有一些不规则数据。 我只是需要将表原样放入我的数据网格中。 我想从数据中捕获“ *(3)”,并使用该数字将以下数据拆分。 Plate Id列可以具有n个ID。 因此,根据该Plate Id的X坐标和Y坐标将被添加到其中。 如何在不干扰其结构的情况下将此表数据放置在数据网格中? 我需要再次使用来自datagrid的数据。
*( 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);
}
}
}
}
尝试以下代码,该代码使用固定宽度的列而不是拆分列
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.