导出 CSV 文件 C#4 时处理引号内的逗号。任何建议
大家好 我正在读取 csv 文件,当字段内有逗号时,我遇到了一个问题
这一切都有效,直到我在逗号内得到一个逗号 所有字段周围都会有“”。
string delimiter=",";
using (var sr = new StreamReader(fileName))
{
sr.ReadLine(); //skip headers
while (!sr.EndOfStream)
{
var readline = sr.ReadLine();
if (readline == null) continue;
var fields = readline.Split (delimiter.ToCharArray());
}
}if I CANNOT split because of commas within quotation . How can I recode it?
我无法使用开源或第三方库。
有什么建议吗?
Hi all
I am reading a csv file and I have encounted a problem when there is a comma inside the field
It all works till I get a comma within a comma
all the fields will have "" around it.
string delimiter=",";
using (var sr = new StreamReader(fileName))
{
sr.ReadLine(); //skip headers
while (!sr.EndOfStream)
{
var readline = sr.ReadLine();
if (readline == null) continue;
var fields = readline.Split (delimiter.ToCharArray());
}
}if I CANNOT split because of commas within quotation . How can I recode it?
I cannot use open source or thirdy party libraries.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
TextFieldParser
类。MSDN上的描述是:
它位于 Microsoft.VisualBasic.FileIO 命名空间中,因此不是第三方也不是开源的。
它是一个托管类,因此您只需添加引用、导入命名空间并使用即可。
用法示例:
Use the
TextFieldParser
class.The description on MSDN is:
It lives in the
Microsoft.VisualBasic.FileIO
namespace, so not third party nor open source.It is a managed class, so you can simply add a reference, import the namespace and use.
Example usage:
由于第 3 方库已退出,因此有一些选择(可能还有其他选择):
Since 3rd party libraries are out, a few choices (there are probably others):