删除从csv文件中读取的原始行中的额外逗号

时间:2018-03-15 02:40:42

标签: c# vb.net csv

我正在开发一个VB.Net代码,我正在从csv文件中读取原始行以供进一步处理。这是csv内容的示例:

I Slewrate Check    3   1   1   IDAC Filter 5uS, OSC dmy 3200pF 0.000005
I Slewrate Check    3   2   1   IDAC Filter 50uS, OSC dmy 3200pF    0.0001
I Slewrate Check    3   3   1   IDAC Filter 200uS, OSC dmy 3200pF   0.00035
I test: 1 mA    18  1   1     -1.15 mA Output   -0.001150366
I test: 1 mA    18  1   2     -1.15 mA Meas. HR ADC -0.001150243

它的外观如下:

enter image description here

这是我阅读csv内容的方式:

For Each rawRows As String In File.ReadLines(diNext.FullName)
                        newList.Clear()
                        processList = rawRows.Split(",")
                        For Each item As String In processList
                            newList.Add(item)
                            'Do something here to save
                        Next

因此,newList将包含5行:

1: I Slewrate Check,    3,  1,  1,  IDAC Filter 5uS, OSC dmy 3200pF,    0.000005
2: I Slewrate Check,    3,  2,  1,  IDAC Filter 50uS, OSC dmy 3200pF,   0.0001
3: I Slewrate Check,    3,  3,  1,  IDAC Filter 200uS, OSC dmy 3200pF,  0.00035
4: I test: 1 mA,    18, 1,  1,    -1.15 mA Output,  -0.001150366
5: I test: 1 mA,    18, 1,  2,    -1.15 mA Meas. HR ADC,    -0.001150243    

前3行在IDAC Filter 5uS, OSC dmy 3200pF中有额外的逗号我不想要。请记住,这只是我的csv的一个示例,额外的逗号可以在任何单元格值处出现,而不仅仅是在图像显示的位置。我该如何解决这个问题。 VB.Net甚至C#中的任何解决方案都会对我有所帮助。我在这里标记C#,为了上帝的缘故,请不要删除C#标签...

在记事本中: enter image description here

1 个答案:

答案 0 :(得分:1)

非常感谢@john ....

这是我使用Microsoft.VisualBasic.FileIO.TextFieldParser

的新功能
Dim parser As TextFieldParser = New TextFieldParser(diNext.FullName)
parser.Delimiters = New String() {","}
parser.HasFieldsEnclosedInQuotes = True
While Not parser.EndOfData
    Try
       Dim fields As String() = parser.ReadFields()
       'Do my processing from here... yahoooo....
    Catch ex As Exception                                
       'Log problem                               
    End Try
End While