本文介绍了如何从csv文件读取特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的sample.csv文件如下所示:

ID名称Loc Sal
1 abc美国2342
2 xyz美国4567
3 QWE欧洲7864
4个空行
5 ASD欧洲4567

现在,我想将所有这些数据写入,但行包含loc作为"usa"到new.csv文件.在我的vb.net winform应用程序中,我编写了将所有行写入新文件的代码,但没有线索来过滤行.我不确定是否可以针对此问题使用相同的逻辑?有什么建议吗?

My sample.csv file looks like this:

ID Name Loc Sal
1 abc usa 2342
2 xyz usa 4567
3 qwe Europe 7864
4 empty row
5 asd Europe 4567

Now I want to write all this data except the rows contains loc as "usa" to new.csv file. In my vb.net winform application I coded for writing all the rows to a new file but haven''t got clue to filter the rows. I am not sure wether I can use same logic for this problem or not? Any suggestions please?

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()

推荐答案

while not ioLine = ""
  ioLine = ioFile.ReadLine
  dim Values as string() = ioLine.split(" ")
  if not values(2) = "usa" then
    iolines += system.environment.newline & ioline
  end if
end while



有关String.split的文档: String.split函数 [ ^ ]



Documentation on String.split: String.split function[^]


这篇关于如何从csv文件读取特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:56