如何拆分其列可能包含的csv,

How to split csv whose columns may contain ,(如何拆分其列可能包含的csv,)
本文介绍了如何拆分其列可能包含的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

如何使用C#将以上信息拆分成字符串如下:

<代码>210162008 年 7 月 31 日 14:22杰夫·达尔加斯2011 年 6 月 5 日 22:21http://stackoverflow.com科瓦利斯,或767935181b437f461b3fd27387c5d8ab47a293d3534

如您所见,其中一列包含 , <= (Corvallis, OR)

更新

基于C# 正则表达式拆分 - 引号外的逗号

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

解决方案

使用 Microsoft.VisualBasic.FileIO.TextFieldParser 类.这将处理解析分隔文件,TextReaderStream,其中一些字段用引号引起来,而另一些则没有.

例如:

使用 Microsoft.VisualBasic.FileIO;字符串 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 解析器 = new TextFieldParser(new StringReader(csv));//你也可以从文件中读取//TextFieldParser parser = new TextFieldParser("mycsvfile.csv");parser.HasFieldsEnclosedInQuotes = true;parser.SetDelimiters(",");字符串 [] 字段;而 (!parser.EndOfData){字段 = parser.ReadFields();foreach(字段中的字符串字段){Console.WriteLine(字段);}}解析器.Close();

这应该会产生以下输出:

<上一页>210162008 年 7 月 31 日 14:22杰夫·达尔加斯2011 年 6 月 5 日 22:21http://stackoverflow.com科瓦利斯,或767935181b437f461b3fd27387c5d8ab47a293d3534

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

您需要在 Add References .NET 选项卡中添加对 Microsoft.VisualBasic 的引用.

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.

这篇关于如何拆分其列可能包含的csv,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)