从文件中删除数据

时间:2011-06-17 16:49:05

标签: .net vb.net

我需要根据列值从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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)

如果您需要加快速度或处理更大的文件,请将文件阅读器包装在一个产生ReadLine()调用的IEnumerable中,而不是使用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