解析文本文件并删除双引号内的逗号

Int*_*eer 4 c# vb.net-2010

我有一个需要转换为 csv 文件的文本文件。我的计划是:

  • 逐行解析文件
  • 搜索并用空格替换双引号内的逗号
  • 然后删除所有双引号
  • 将行附加到新的 csv 文件

问题:我需要一个函数来识别双引号内的逗号并替换它。

这是一个示例行:

“布朗夫人”、“博蒙特街 4611 号”、“勇士跑”、“PA”

Pra*_*mar 5

您的文件似乎已经采用 CSV 投诉格式。任何好的 CSV 阅读器都可以正确阅读。

如果您的问题只是正确读取字段值,那么您需要以正确的方式读取它。

这是一种方法:

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();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

我刚刚注意到该问题已在 C#、VB.NET-2010 下提交。这是 VB.NET 版本,以防万一您在 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
Run Code Online (Sandbox Code Playgroud)