-
Aug 2nd, 2021, 03:06 AM
#1
Thread Starter
Addicted Member
[RESOLVED] VB.net delete rows from .csv where value = 0
I am trying to delete all rows from a .csv where a column is 0.
I use a streamreader to read the lines and then streamwriter to create a new file.
However the rows are not being deleted.
Code:
Dim SR As New StreamReader(extractPath & GebruikHierdieCSV & ".csv")
Dim SWriter As New StreamWriter(extractPath & "nuwe verkorte csv file.csv")
Dim strLineData() As String
Do While SR.Peek <> -1
strLineData = Split(SR.ReadLine, ",")
If strLineData(16) <> "0" Then
SWriter.WriteLine(strLineData(0) & "," & strLineData(1) & "," & strLineData(3) & "," & strLineData(6) & "," & strLineData(9) & "," & strLineData(10) & "," & strLineData(11) & "," & strLineData(13) & "," & strLineData(16) & "," & strLineData(18))
End If
Loop
SR.Close()
SWriter.Close()
MessageBox.Show("File has been created and can be located at " & extractPath & "nuwe verkorte csv file.csv")
I only write certain columns to the new .csv file.
-
Aug 2nd, 2021, 03:23 AM
#2
Re: VB.net delete rows from .csv where value = 0
That code doesn't delete anything. What it does is read data and then write it to a new file and filter out some of the original data. Are you actually saying that every line from the original file has a corresponding line in the new file?
Have you actually debugged the code? I can only assume that you haven't because any issues would be obvious if you had. Set a breakpoint and step through the code line by line and actually look at the data you're processing as you process it. If you don't know how to use the debugger then stop what you're doing and learn now.
Apart from using the tools provided by your IDE to actually do development, you can clean that code up considerably too:
vb.net Code:
Using writer As New StreamWriter(Path.Combine(extractPath, "nuwe verkorte csv file.csv")
For Each line In File.ReadLines(Path.Combine(extractPath, GebruikHierdieCSV & ".csv")
Dim fields = line.Split(","c)
If fields(16) <> "0" Then
writer.WriteLine($"{strLineData(0)},{strLineData(1)},{strLineData(3)},{strLineData(6)},{strLineData(9)},{strLineData(10)},{strLineData(11)},{strLineData(13)},{strLineData(16)},{strLineData(18)}")
End If
Next
End Using
It's worth noting that both your original code and this code will not account for data that contains columns within the data. If that's something you need, you should look at using a TextFieldParser instead.
-
Aug 2nd, 2021, 05:58 AM
#3
Thread Starter
Addicted Member
Re: VB.net delete rows from .csv where value = 0
Thank you jmchilhinney.
Your code is much cleaner than mine.
Your question
Are you actually saying that every line from the original file has a corresponding line in the new file?
provided the answer.
The solution was simple really. The data in the .csv is "0.0000" and not "0".
So changing the line
Code:
If fields(16) <> "0.0000" Then
did the trick.
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
|