如何分割CSV其列可能包含,CSV

2023-09-02 10:26:15 作者:我在风中等你

由于

       

2,1016,7 /二千〇八分之三十一14:22,杰夫达尔加斯,6/5/2011,22:21,HTTP://stackoverflow.com,科瓦利斯,OR,7679,351,81,b437f461b3fd27387c5d8ab47a293d35, 34

  

如何使用C#来分割上述信息转换成字符串如下:

  2
1016
7/31/2008 14:22
杰夫达尔加斯
2011年6月5日22:21
http://stackoverflow.com
科瓦利斯,OR
7679
351
81
b437f461b3fd27387c5d8ab47a293d35
34
 

正如你可以看到一个列包含,< =(科瓦利斯,OR)

//更新// 基于 C#正则表达式斯普利特 - 逗号以外报价

 的String []的结果= Regex.Split(samplestring,(=(?:?[^ ] * [^ ] * )* [ ^ ] * $));
 

解决方案 什么是 CSV 文件,如何打开它

使用了 Microsoft.VisualBasic.FileIO.TextFieldParser 类。这将处理解析一个分隔的文件,的TextReader ,其中某些字段用引号括起来,有些则不是。

例如:

 使用Microsoft.VisualBasic.FileIO;

字符串CSV =2,1016,7 /二千○八分之三十一14:22,杰夫达尔加斯,6/5/2011,22:21,HTTP://stackoverflow.com,科瓦利斯,或,7679,351,81 ,b437f461b3fd27387c5d8ab47a293d35,34;

TextFieldParser分析器=新TextFieldParser(新StringReader(CSV));

//你也可以从文件中读取
// TextFieldParser分析器=新TextFieldParser(mycsvfile.csv);

parser.HasFieldsEnclosedInQuotes = TRUE;
parser.SetDelimiters(,);

字符串[]字段;

而(!parser.EndOfData)
{
    字段= parser.ReadFields();
    的foreach(在字段中串场)
    {
        Console.WriteLine(场);
    }
}

parser.Close();
 

这应该导致下面的输出:

2
1016
7/31/2008 14:22
杰夫达尔加斯
2011年6月5日22:21
http://stackoverflow.com
科瓦利斯,OR
7679
351
81
b437f461b3fd27387c5d8ab47a293d35
34

请参阅Microsoft.VisualBasic.FileIO.TextFieldParser了解更多信息。

您需要添加一个引用到 Microsoft.VisualBasic程序在添加引用.NET选项卡。

Given

2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34

How to use C# to split the above information into strings as follows:

2
1016
7/31/2008 14:22
Geoff Dalgas
6/5/2011 22:21
http://stackoverflow.com
Corvallis, OR
7679
351
81
b437f461b3fd27387c5d8ab47a293d35
34

As you can see one of the column contains , <= (Corvallis, OR)

// update // Based on C# Regex Split - commas outside quotes

string[] result = Regex.Split(samplestring, ",(?=(?:[^"]*"[^"]*")*[^"]*$)");

解决方案

Use the Microsoft.VisualBasic.FileIO.TextFieldParser class. This will handle parsing a delimited file, TextReader or Stream where some fields are enclosed in quotes and some are not.

For example:

using Microsoft.VisualBasic.FileIO;

string csv = "2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34";

TextFieldParser parser = new TextFieldParser(new StringReader(csv));

// You can also read from a file
// TextFieldParser parser = new TextFieldParser("mycsvfile.csv");

parser.HasFieldsEnclosedInQuotes = true;
parser.SetDelimiters(",");

string[] fields;

while (!parser.EndOfData)
{
    fields = parser.ReadFields();
    foreach (string field in fields)
    {
        Console.WriteLine(field);
    }
} 

parser.Close();

This should result in the following output:

2
1016
7/31/2008 14:22
Geoff Dalgas
6/5/2011 22:21
http://stackoverflow.com
Corvallis, OR
7679
351
81
b437f461b3fd27387c5d8ab47a293d35
34

See Microsoft.VisualBasic.FileIO.TextFieldParser for more information.

You need to add a reference to Microsoft.VisualBasic in the Add References .NET tab.