I have a txt file with the following data:
|
I'd like to read that file, remove the header, remove col1 ......col4.
So, at the end I'd like to have the following data in a two-dimensional array so that I can manipulate (copy, sort...) them:
|
I've found on this and other forums how to read the txt, but no solutions how to remove all these unwanted lines.
I'm reading the file so:
|
Thanks for your help
Kabanga
Zoran HorvatPosted Nov 16, 2010, 9:28 AM
Here is the code which does that - type lines and finish with a line consisting of single dash (-):
class Program
{
static void Main(string[] args)
{
List list = new List();
while (true)
{
string line = Console.ReadLine();
if (line == "-")
break;
else list.Add(line);
}
string[] lines = list.ToArray();
string[][] cells = new string[lines.Length][];
for (int i = 0; i < cells.Length; i++)
cells[i] = lines[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("------------------------------------");
for (int i = 0; i < cells.Length; i++)
{
for (int j = 0; j < cells[i].Length; j++)
Console.Write("[{0}] ", cells[i][j]);
Console.WriteLine();
}
Console.WriteLine("------------------------------------");
}
}
Input:
Output:
------------------------------------
[line1]
[line2]
[line3]
[col1] [col2] [col3] [col4]
[1] [3] [12] [63]
[83] [10] [19] [14]
[21] [34] [87] [54]
------------------------------------
You can skip headers printing by starting the printout loop from specified index (e.g. 1 instead of 0).
JoPosted Nov 17, 2010, 6:06 AM
Using the method of Zoran I could read the data into a 2d array.
Sam, I like your idea. Combining both Zoran's and your idea I'd get exactly what I want.
What I'm doing now is manually counting the lines till the beginning of numeric data.
With that method, if the number of lines changes, I must manually change the code to adapt to the new situation.
Before the beginning of numeric data in the txt file there is always the string " End Comments".
I'm wondering if there is any function like "ScanLine( path, stringToFind)" that I'll use for detecting the string " End Comments".
Best regards
Sam HobbsPosted Nov 17, 2010, 5:13 AM
Jean PaulPosted Nov 16, 2010, 9:27 AM
and discard the lines containing alphabets
Again you can split based on space and get the values