CSV文本文件分析器与TextFieldParser

编程入门 行业动态 更新时间:2024-10-28 21:16:55
本文介绍了CSV文本文件分析器与TextFieldParser - MalformedLineException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个CSV解析器使用C#的 TextFieldParser 类。

I am working on a CSV parser using C# TextFieldParser class.

我的CSV数据是由,和该字符串由字符括起来。

My CSV data is deliminated by , and the string is enclosed by a " character.

不过,有时数据行的单元格还可以有一个这似乎使得解析器抛出一个异常

However, sometimes the data row cell can also have a " which appears to be making the parser throw an exception.

这是我的C#代码至今:

This is my C# code so far:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Microsoft.VisualBasic.FileIO; namespace CSV_Parser { class Program { static void Main(string[] args) { // Init string CSV_File = "test.csv"; // Proceed If File Is Found if (File.Exists(CSV_File)) { // Test Parse_CSV(CSV_File); } // Finished Console.WriteLine("Press any to exit ..."); Console.ReadKey(); } static void Parse_CSV(String Filename) { using (TextFieldParser parser = new TextFieldParser(Filename)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); parser.TrimWhiteSpace = true; while (!parser.EndOfData) { string[] fieldRow = parser.ReadFields(); foreach (string fieldRowCell in fieldRow) { // todo } } } } } }

这是对我的内容 test.csv 文件:

" dummy test"s data", b , c d,e,f gh,ij

什么是对付最好的办法 在我行单元格的数据?

What is the best way to deal with " in my row cell data?

更新

根据添Schmelter的的回答,我已经修改我的代码如下:

Based on Tim Schmelter's answer, I have modified my code to the following:

static void Parse_CSV(String Filename) { using (TextFieldParser parser = new TextFieldParser(Filename)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); parser.HasFieldsEnclosedInQuotes = false; parser.TrimWhiteSpace = true; while (parser.PeekChars(1) != null) { var cleanFieldRowCells = parser.ReadFields().Select( f => f.Trim(new[] { ' ', '"' })); Console.WriteLine(String.Join(" | ", cleanFieldRowCells)); } } }

这似乎产生以下(正常):

Which appears to produce the following (correctly):

时这是处理字符串用引号括起来,有引号的最佳方式?

Is this is the best way to deal with string enclosed by quotes, having quotes?

推荐答案

能否通过设置省略引用字符 HasFieldsEnclosedInQuotes 到假?

Could you omit the quoting-character by setting HasFieldsEnclosedInQuotes to false?

using (var parser = new TextFieldParser(@"Path")) { parser.HasFieldsEnclosedInQuotes = false; parser.Delimiters = new[]{","}; while(parser.PeekChars(1) != null) { string[] fields = parser.ReadFields(); } }

您可以手动删除引号:

var cleanFields = fields.Select(f => f.Trim(new[]{ ' ', '"' }));

更多推荐

CSV文本文件分析器与TextFieldParser

本文发布于:2023-05-26 13:03:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/259572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分析器   文本文件   CSV   TextFieldParser

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!