2011-06-17 25 views
0

我需要根據列值從csv文件中刪除一些行。這是我的sample.csv文件從文件中刪除數據

Name  no  date  sal 
abc  12  06/06/10 2345 
xyz  11  06/06/10 2321 
qwe  10  05/07/10 4323 
asd  10  05/07/10 3221 

在我的vb.net winforom的應用程序,我要讀這個文件,並需要刪除相關06/06/10日期列值的行,然後寫剩餘的行成new.csv文件。到目前爲止,我的程序在讀取文件中存在的全部數據。對此有任何建議嗎?

這裏是我的代碼:

Dim ioFile As New System.IO.StreamReader("C:\sample.csv")  
Dim ioLine As String 
Dim ioLines As String  
ioLine = ioFile.ReadLine  
ioLines = "ID,Name,Number,Amount" 
ioLines &= vbCrLf & ioLine 
While Not ioLine = ""   
    ioLine = ioFile.ReadLine   
    ioLines = ioLines & vbCrLf & ioLine  
End While  
Dim ioWriter As New System.IO.StreamWriter("C:\new.csv")  
ioWriter.WriteLine(ioLines)  
ioFile.Close()  
ioWriter.Close() 

回答

2

希望這將讓你開始(我假定固定列寬,似乎是從您的示例文件的情況下):

Const dateColumnOffset As Integer = 16 ' I hope I counted that right 
Const dateColumnWidth As Integer = 8 
Using writer As StreamWriter = File.CreateText("new.csv") 
    For Each line As String In File.ReadLines("sample.csv") 
     Dim recordDate As String = line.Substring(dateColumnOffset, dateColumnWidth) 
     If recordDate <> "06/06/10" Then 
      writer.WriteLine(line) 
     End If 
    Next 
End Using 
+0

列值不會有任何固定長度。爲了提供示例csv文件,我給出了固定長度的數據。我怎樣才能做到不同長度? – Ram

+0

舉一個你的實際數據的例子,也許有人可以幫忙。 – Sven

0

這是一個非常天真和簡單的解決方案,並假定您正在處理一個非常小的文件,您不關心性能,並且.csv數據本身從不包含逗號。但是,因爲你沒有聲明,否則,我們假設完成工作是我們的目標,這將做到這一點:如果您需要加快這或處理更大的文件,包裹你的文件閱讀器

Dim data() As String = System.IO.File.ReadAllLines("C:\Temp\sample.csv") 
Dim newData = From a In data Where DateTime.Parse(a.Split(","c)(2)) <> #6/10/10# 
System.IO.File.WriteAllLines("C:\Temp\new.csv", newData) 

在IEnumerable中產生ReadLine()調用,而不是一次用ReadAllLines()將整個文件讀入內存。如果你的.CSV在數據中會引用標識符和逗號,那麼你需要使用更高級的.CSV閱讀器。微軟建立了一個。See here

- 編輯 -

每請求時,這裏是你如何在VB做一個文件行枚舉。使用該類代碼更改爲From a In New TextFileEnumerator("C:\Temp\sample.csv") Where...

''' Handy helper class for iterating over lines in a text file. Lets you do something like a 
''' Linq-to-files. 
''' </summary> 
Public Class TextFileEnumerator 
    Implements IEnumerator(Of String), IEnumerable(Of String) 

    Private _filePath As String 
    Private _rdr As StreamReader 
    Private _line As String 

    Private ReadOnly Property StreamReader() As StreamReader 
     Get 
     _rdr = If(_rdr, New StreamReader(_filePath)) 
     Return _rdr 
     End Get 
    End Property 

    Public Sub New(filePath As String) 
     If Not System.IO.File.Exists(filePath) Then Throw New FileNotFoundException(filePath) 
     _filePath = filePath 
    End Sub 

    Public ReadOnly Property Current As String Implements System.Collections.Generic.IEnumerator(Of String).Current 
     Get 
     Return _line 
     End Get 
    End Property 

    Public ReadOnly Property Current1 As Object Implements System.Collections.IEnumerator.Current 
     Get 
     Return _line 
     End Get 
    End Property 

    Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext 
     _line = Me.StreamReader.ReadLine() 
     Return (_line IsNot Nothing) 
    End Function 

    Public Sub Reset() Implements System.Collections.IEnumerator.Reset 
     If _rdr IsNot Nothing Then 
     _rdr.Close() 
     End If 
     _rdr = Nothing 
    End Sub 

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of String) Implements System.Collections.Generic.IEnumerable(Of String).GetEnumerator 
     Return Me 
    End Function 

    Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator 
     Return Me 
    End Function 

#Region "IDisposable Support" 
    Private disposedValue As Boolean ' To detect redundant calls 

    ' IDisposable 
    Protected Overridable Sub Dispose(disposing As Boolean) 
     If Not Me.disposedValue Then 
     If disposing Then 
      If _rdr IsNot Nothing Then 
       _rdr.Close() 
       _rdr.Dispose() 
       _rdr = Nothing 
      End If 
     End If 

     ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. 
     ' TODO: set large fields to null. 
     End If 
     Me.disposedValue = True 
    End Sub 

    ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources. 
    'Protected Overrides Sub Finalize() 
    ' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. 
    ' Dispose(False) 
    ' MyBase.Finalize() 
    'End Sub 

    ' This code added by Visual Basic to correctly implement the disposable pattern. 
    Public Sub Dispose() Implements IDisposable.Dispose 
     ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. 
     Dispose(True) 
     GC.SuppressFinalize(Me) 
    End Sub 
#End Region 

End Class 
+0

由於我對編程非常陌生,您能否使用IEnumerable爲此問題提供示例代碼? – Ram