c# - Parsing Tab delimited text files -
i have tab delimited file columns , rows example: rows might not have value columns. know "order" doesn't change third tab delimited thing column3 , on.
column1 column2 column3 .... column12 .... column34 ... column50 123 34 abc 234 def as@ddd.com true 45 nyc wwe@dsds.com false
now need read file not of columns important program. example need stuff values in column2, column12,column45
what approach suggest?
try following approach
static void main(string[] args) { datatable datatable = new datatable(); streamreader streamreader = new streamreader(@"c:\temp\txt.txt"); char[] delimiter = new char[] { '\t' }; string[] columnheaders = streamreader.readline().split(delimiter); foreach (string columnheader in columnheaders) { datatable.columns.add(columnheader); // i've added column headers here. } while (streamreader.peek() > 0) { datarow datarow = datatable.newrow(); datarow.itemarray = streamreader.readline().split(delimiter); datatable.rows.add(datarow); } foreach (datarow row in datatable.rows) { console.writeline(""----row no: " + datatable.rows.indexof(row) + "----""); foreach (datacolumn column in datatable.columns) { //check columns need if (column.columnname == "column2" || column.columnname == "column12" || column.columnname == "column45") { console.write(column.columnname); console.write(" "); console.writeline(row[column]); } } } console.readline(); }
Comments
Post a Comment