|
-
Dec 13th, 2009, 06:31 PM
#1
Thread Starter
Fanatic Member
Remove empty columns from a tab delimited text file?
How can I open a tab delimited text file and remove the columns that are empty and the save it?
Empty columns are those that have no value in the first row.
-
Dec 13th, 2009, 07:00 PM
#2
Re: Remove empty columns from a tab delimited text file?
I'd suggest reading the data into a DataTable, testing the data in the first row, removing the appropriate DataColumns, then writing the data back out to the file. You could use ADO.NET to read the data, or a TextFieldParser, or a StreamReader. I'd go with the TextFieldParser. You'd then use a StreamWriter to write the data back out again in a nested For or For Each loops.
-
Dec 14th, 2009, 02:32 AM
#3
Thread Starter
Fanatic Member
Re: Remove empty columns from a tab delimited text file?
Thanks jmcilhinney,
Do you have any examples you can show?
-
Dec 14th, 2009, 04:15 AM
#4
Thread Starter
Fanatic Member
Re: Remove empty columns from a tab delimited text file?
Ok, this imports a text file to its data table.
But how can I delete these empty columns? And how can I then export back the text file? Can I export with other delemiters also?
vb.net Code:
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal seperator As Char) As DataTable
Dim myTable As DataTable = New DataTable("MyTable")
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields there are.
myReader = f.OpenText(fileFullPath)
fieldValues = myReader.ReadLine().Split(seperator)
'Create data columns accordingly
For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Field" & i))
Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(seperator)
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return New DataTable("Empty")
Finally
myReader.Close()
End Try
Return myTable
End Function
Call it with
vb.net Code:
BuildDataTable("C:\Testfile.txt", vbTab)
Last edited by Pirre001; Dec 14th, 2009 at 04:23 AM.
-
Dec 14th, 2009, 06:40 AM
#5
Thread Starter
Fanatic Member
Re: Remove empty columns from a tab delimited text file?
-
Dec 14th, 2009, 10:28 AM
#6
Junior Member
Re: Remove empty columns from a tab delimited text file?
You added columns with myTable.Columns.Add
You should be able to drop the ones you don't want with Remove, e.g. to drop column 3, myTable.Columns.Remove(3)
Remember they count from 0, of course.
-
Dec 14th, 2009, 12:34 PM
#7
Thread Starter
Fanatic Member
Re: Remove empty columns from a tab delimited text file?
Ok, but how can I drop empty columns? What I mean is, how can I figure out wich columns who are empty? That's the problem...
-
Dec 14th, 2009, 03:08 PM
#8
Junior Member
Re: Remove empty columns from a tab delimited text file?
You defined an empty column as "Empty columns are those that have no value in the first row.". So you need to test every field in the first row to see if it's empty (Null).
Something like
Code:
myRow = myTable.Rows(0)
For i = myTable.Columns.Count - 1 To 0 Step -1
If myRow.Item(i).isNull Then myTable.Columns.Remove(i)
Next i
ought to work (no I have not tested it).
Note that it's stepping down from the last column, so it only removes columns you've already checked.
There are ways to solve your problem without building tables at all. You could read in the first row, set an array GoodCol() as Boolean to True or False depending whether each field is empty, then read in and split each row, writing out only the fields for which GoodCol(i) was true.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|