解析文本文件并删除双引号内的逗号
我有一个需要转换为 csv 文件的文本文件.我的计划是:
I have a text file that needs to be converted into a csv file. My plan is to:
- 逐行解析文件
- 搜索并用空格替换双引号内的逗号
- 然后删除所有双引号
- 将该行附加到一个新的 csv 文件
问题:我需要一个函数来识别双引号内的逗号并替换它.
Question: I need a function that will recognize the comma inside a double quote and replace it.
这是一个示例行:
"MRS Brown","4611 BEAUMONT ST","","WARRIOR RUN, PA"
"MRS Brown","4611 BEAUMONT ST"," ","WARRIOR RUN, PA"
您的文件似乎已经是 CSV 投诉格式.任何优秀的 CSV 阅读器都可以正确阅读.
Your file seems to be already in a CSV complaint format. Any good CSV reader would be able to read it correctly.
如果您的问题只是正确读取字段值,那么您需要以正确的方式读取它.
If your problem is just reading the field values correctly, then you need to read it the correct way.
这是一种方法:
using Microsoft.VisualBasic.FileIO;
private void button1_Click(object sender, EventArgs e)
{
TextFieldParser tfp = new TextFieldParser("C:\\Temp\\Test.csv");
tfp.Delimiters = new string[] { "," };
tfp.HasFieldsEnclosedInQuotes = true;
while (!tfp.EndOfData)
{
string[] fields = tfp.ReadFields();
// do whatever you want to do with the fields now...
// e.g. remove the commas and double-quotes from the fields.
for (int i = 0; i < fields.Length;i++ )
{
fields[i] = fields[i].Replace(","," ").Replace("\"","");
}
// this is to show what we got as the output
textBox1.AppendText(String.Join("\t", fields) + "\n");
}
tfp.Close();
}
我刚刚注意到该问题已在 C#、VB.NET-2010 下提交.这是 VB.NET 版本,以防万一您在 VB 中编码.
I just noticed that the question has been filed under C#, VB.NET-2010. Here is the VB.NET version, just in case you are coding in VB.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tfp As New FileIO.TextFieldParser("C:\Temp\Test.csv")
tfp.Delimiters = New String() {","}
tfp.HasFieldsEnclosedInQuotes = True
While Not tfp.EndOfData
Dim fields() As String = tfp.ReadFields
'' do whatever you want to do with the fields now...
'' e.g. remove the commas and double-quotes from the fields.
For i As Integer = 0 To fields.Length - 1
fields(i) = fields(i).Replace(",", " ").Replace("""", "")
Next
'' this is to show what we got as the output
TextBox1.AppendText(Join(fields, vbTab) & vbCrLf)
End While
tfp.Close()
End Sub