A simple CSV parser based on Microsoft.VisualBasic.FileIO.TextFieldParser.
![Build status](https://github.com/22222/CsvTextFieldParser/actions/workflows/build.yml/badge.svg)
The goal of this library is to provide a simple alternative to the `TextFieldParser` class in the `Microsoft.VisualBasic` assembly that works with .NET Core, or just that doesn't require you to use a VisualBasic assembly to a C# project.
Installation
============
There's just one real file in this library: [CsvTextFieldParser](CsvTextFieldParser/CsvTextFieldParser.cs). So the recommended way to use this is to just copy that file into your project.
This project is available under either of two licenses: [MIT](LICENSE) or [The Unlicense](UNLICENSE). If you're able to use the unlicense, then you don't have to provide any attribution at all. You want to throw this code into your own project and tell everyone you wrote it? Go for it.
Alternatively, there are a couple more traditional ways to use this library:
* Install the [NuGet package](https://www.nuget.org/packages/CsvTextFieldParser/)
* Download the assembly from the [latest release](https://github.com/22222/CsvTextFieldParser/releases/latest) and reference it manually
Getting Started
===============
Using this library is almost exactly the same as using the VisualBasic `TextFieldParser` class. Here's an example of using this library to parse CSV input into dictionaries:
```csharp
public static IEnumerable<IDictionary<string, string>> ParseCsvWithHeader(string csvInput)
{
using (var csvReader = new StringReader(csvInput))
using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(csvReader))
{
if (parser.EndOfData)
{
yield break;
}
string[] headerFields = parser.ReadFields();
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
int fieldCount = Math.Min(headerFields.Length, fields.Length);
IDictionary<string, string> fieldDictionary = new Dictionary<string, string>(fieldCount);
for (var i = 0; i < fieldCount; i++)
{
string headerField = headerFields[i];
string field = fields[i];
fieldDictionary[headerField] = field;
}
yield return fieldDictionary;
}
}
}
```
But let's take a step back. You've got some CSV data you need to parse:
```csv
Name,Birth Date
Apollo Creed,1942-08-17
Ivan Drago,1961-11-03
```
That's easy, it only takes a few lines of code:
```csharp
public static IEnumerable<string[]> ParseBasicCsv(string input)
{
using (var reader = new StringReader(input))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (!string.IsNullOrEmpty(line))
{
yield return line.Split(',');
}
}
}
}
public static void ProcessCsv(string csvInput)
{
foreach (var csvLine in ParseBasicCsv(csvInput).Skip(1))
{
var name = csvLine[0];
var birthDate = csvLine[1];
Console.WriteLine($"{name} was born on {birthDate}");
}
}
```
If that's the kind of CSV data you have, then throw this library in the garbage and just use that. Parsing CSV data is simple when your values will never contain any characters that require special handling.
But wait, now they've changed the format of the CSV file. Some of the values contain commas, so now you have to put those values in quotes:
```csv
Name,Birth Date
"Creed, Apollo",1942-08-17
Ivan Drago,1961-11-03
```
Everything's a little more complicated now And maybe it turns out they sometimes put newlines in those quoted values, and some of the quoted values also have quote characters in them:
```csv
Name,Birth Date
"Creed, Apollo",1942-08-17
"Ivan
Drago",1961-11-03
"Robert ""Rocky"" Balboa",1945-07-06
```
Now maybe it's time to consider a CSV library. And what luck, the .NET standard library happens to have one built-in. Here's your code now:
```csharp
public static void ProcessCsv(string csvInput)
{
using (var csvReader = new StringReader(csvInput))
using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(csvReader))
{
parser.SetDelimiters(",");
parser.TrimWhiteSpace = false;
// Skip the header line
if (!parser.EndOfData) parser.ReadFields();
while (!parser.EndOfData)
{
var csvLine = parser.ReadFields();
var name = csvLine[0];
var birthDate = csvLine[1];
Console.WriteLine($"{name} was born on {birthDate}");
}
}
}
```
That works great. It's kind of a weird library, but it handles the quoting thing so you can have commas and newlines in your fields now. So once again, this library can go straight into the garbage.
But there's one thing that's kind of gross about this solution: we have to add a dependency on the `Microsoft.VisualBasic` assembly to our C# project. It's still .NET, so that should all work fine. But it feels a little wrong, doesn't it?
And there's another problem. The `Microsoft.VisualBasic` probably isn't in .NET Standard, and probably won't ever be. So if you want CSV parsing in a .NET Core application, you're probably back to having to pick a library.
One option is to use this one. It's almost exactly the same as using the VisualBasic TextFieldParser:
```csharp
public static void ProcessCsv(string csvInput)
{
using (var csvReader = new StringReader(csvInput))
using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(csvReader))
{
// Skip the header line
if (!parser.EndOfData) parser.ReadFields();
while (!parser.EndOfData)
{
var csvLine = parser.ReadFields();
var name = csvLine[0];
var birthDate = csvLine[1];
Console.WriteLine($"{name} was born on {birthDate}");
}
}
}
```
So you can start with either this parser or the VisualBasic one, and you should be able to freely switch between them with only minimal code changes.
Error Handling
==============
There are two types of input that can cause an error when parsing a CSV file:
* A quoted field has trailing characters after the closing quote character
* A quoted field is started but never closed by another unescaped quote character
Either of these cases will cause a `CsvMalformedLineException` to be thrown.
Getting information about these errors works just like handling `Microsoft.VisualBasic.FileIO.MalformedLineException` exceptions from the `Microsoft.VisualBasic.FileIO.TextFieldParser` class:
* The exception contains a `LineNumber` property that tells you the number of the line in the file is malformed
* The parser class contains an `ErrorLine` property that provides the actual malformed line
* The parser class also contains an `ErrorLineNumber` property with the same value as the exception's `LineNumber` property
Here's an example of using this parser with error handling:
```csharp
public static IEnumerable<IDictionary<string, string>> ParseCsvWithHeaderIgnoreErrors(string csvInput)
{
using (var csvReader = new StringReader(csvInput))
using (var parser = new NotVisualBasic.FileIO.CsvTextFieldParser(csvReader))
{
if (parser.EndOfData)
{
yield break;
}
string[] headerFields;
try
{
headerFields = parser.ReadFields();
}
catch (NotVisualBasic.FileIO.CsvMalformedLineException ex)
{
Console.Error.WriteLine($"Failed to parse header line {ex.LineNumber}: {parser.ErrorLine}");
yield break;
}
while (!parser.EndOfData)
{
string[] fields;
try
{
fields = parser.ReadFields();
}
catch (NotVisualBasic.FileIO.CsvMalformedLineException ex)
{
Console.Error.WriteLine($"Failed to parse line {ex.LineNumber}: {parser.ErrorLine}");
continue;
}
int fieldCount = Math.Min(headerFields.Length, fields.Length);
IDictionary<string, string> fieldDictionary = new Dictionary<string, string>(fieldCount);
for (var i = 0; i < fieldCount; i++)
{
string headerField = headerFields[i];
string field = fields[i];
fieldDictionary[headerField] =
没有合适的资源?快使用搜索试试~ 我知道了~
基于 Microsoft.VisualBasic.FileIO.TextFieldParser 的简单 CSV 分析器
共24个文件
cs:11个
csproj:3个
unlicense:1个
需积分: 5 0 下载量 73 浏览量
2024-03-17
15:35:12
上传
评论
收藏 33KB ZIP 举报
温馨提示
1、资源内容:基于 Microsoft.VisualBasic.FileIO.TextFieldParser 的简单 CSV 分析器 2、适用人群:计算机,电子信息工程、数学等专业的学习者,作为java实战项目,课程设计,毕业设计“参考资料”参考学习使用。 3、解压说明:本资源需要电脑端使用WinRAR、7zip等解压工具进行解压,没有解压工具的自行百度下载即可。
资源推荐
资源详情
资源评论
收起资源包目录
基于 Microsoft.VisualBasic.FileIO.TextFieldParser 的简单 CSV 分析器.zip (24个子文件)
new-2
UNLICENSE 1KB
.editorconfig 2KB
Directory.Build.props 554B
CsvTextFieldParser
CsvTextFieldParser.csproj 1KB
CsvTextFieldParser.cs 24KB
CsvTextFieldFormatter.cs 11KB
.github
workflows
build.yml 2KB
.gitattributes 7B
LICENSE 1KB
CsvTextFieldParser.sln 3KB
CsvTextFieldParser.SampleConsole
CsvTextFieldParser.SampleConsole.csproj 561B
App.config 186B
Program.cs 12KB
CsvTextFieldParser.Tests
CsvTextFieldParserTest.cs 44KB
CsvTextFieldFormatterTest.cs 13KB
ComparisonWithVbTextFieldParserTest.cs 10KB
CsvTextFieldParserTest_Configuration.cs 4KB
CsvTextFieldParserTest_CompatibilityMode.cs 546B
CsvTextFieldParser.Tests.csproj 882B
VbCsvTextFieldParserTest.cs 3KB
ComparisonWithBasicParserTest.cs 2KB
CustomAssert.cs 2KB
.gitignore 5KB
README.md 13KB
共 24 条
- 1
资源评论
白话Learning
- 粉丝: 3275
- 资源: 2464
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 下载安装这个软件.apk
- 【数据集详细解释及案例分析】数据集详细解释及案例分析
- 基于SHT71温湿度传感器、STM32F103C8T6、LCD1602温湿度采集显示系统proteus仿真设计
- 基于TH02温湿度传感器、STM32F103C8T6、LCD1602、FREERTOS的温湿度采集系统proteus仿真设计
- 【TCP-IP协议详细解释及案例分析】TCP-IP协议详细解释及案例分析
- 一文搞懂 LSTM(长短期记忆网络).rar
- 【autosar简介及基本案例解析】autosar简介及基本案例解析
- java模拟斗地主洗牌发牌
- springboot+vue登录系统 vue部分
- 常用常见 SQL语句语法
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功