我的文件包含以下格式的数据:
Name,Sal,Address,Location
Mike,"£10,732",xxxxxx,GBR
Bob,"£6,450",xxxxxxx,Fra
Arthur,"£8,320",xxxxx,Spa
James,"£7,423",xxxxxxxxxxxx,IRE
我需要将这些数据读入字符串数组。在我的新文件中,我只需要编写Name,Sal和Location列。这是我的代码:
Dim ioReader As New System.IO.Streamreader("C:\old.csv")
ioLines="Name,Sal,Location"
Dim ioWriter As New System.IO.StreamWriter("C:\new.csv")
While Not ioLine = ""
ioLine = ioFile.ReadLine
Dim values As String()
If ioLine <> "" Then
values = ioLine.Split(",")
Dim outPut as string=values(0) & values(1) & values(3)
ioLines += System.Environment.NewLine & outPut
EndIf
当我分割上述数据时,已包含“,”的sal列值将分成2个单元格。我想通过忽略数字之间的“,”将sal列值保持为单个单元格。有什么建议吗?
答案 0 :(得分:2)
看起来你正在解析CSV,如果是,你可以使用
Microsoft.VisualBasic.FileIO.TextFieldParser
e.g。
Using MyReader As New Microsoft.VisualBasic.FileIO.
TextFieldParser("c:\logs\bigfile")
MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","c}
MyReader.HasFieldsEnclosedInQuotes = True
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
" is invalid. Skipping")
End Try
End While
End Using
答案 1 :(得分:1)
快速而肮脏的解决方案:
Dim outPut as string=values(0) & values(1) & "," & values(2) & values(5)
输入文件不是正确的CSV - ,
的字段应该被转义(用"
包围):
Name,Sal,Address,Location
Mike,"£10,732",xxxxxx,GBR
Bob,"£6,450",xxxxxxx,Fra
Arthur,"£8,320",xxxxx,Spa
James,"£7,423",xxxxxxxxxxxx,IRE
我不会使用string.Split
而是使用CSV解析器 - 您可以使用TextFieldParser
类。
答案 2 :(得分:0)
又快又脏......
using System.Text.RegularExpressions;
private void Test(){
Regex rex = new Regex(@"(?<!£\d*),");
string[] s = rex.Split(@"Name,Sal,Address,Location Mike,£10,732,xxxxxx,GBR Bob,£6,450,xxxxxxx,Fra Arthur,£8,320,xxxxx,Spa James,£7,423,xxxxxxxxxxxx,IRE");
}
正则表达式不会以逗号分隔,而是以井号和数字开头。但是,这只允许使用货币中的一个逗号,因此£10,342,234
会破坏它...