.Net开源CSV文件处理组件学习

CSV文件是比较常用的数据存储格式,.net系统下csv组件是一个短小简洁的开源组件,项目地址:https://github.com/stevehansen/csv

我有一种感觉,短小精悍的解决特定问题的部件要优于比较复杂的框架。

读取的示例(甚至没有从文件中读取的函数,充分体现了简洁的设计):

var csv = File.ReadAllText("sample.csv");
foreach (var line in CsvReader.ReadFromText(csv))
{
    // Header is handled, each line will contain the actual row data
    var firstCell = line[0];
    var byName = line["Column name"];
}

写入示例:

var columnNames = new [] { "Id", "Name" };
var rows = new [] 
{
    new [] { "0", "John Doe" },
    new [] { "1", "Jane Doe" }
};
var csv = CsvWriter.WriteToText(columnNames, rows, ,);
File.WriteAllText("people.csv", csv);

还有一些处理选项(下面是缺省的):

var options = new CsvOptions // Defaults
{
    RowsToSkip = 0, // 开头跳过的行数
    SkipRow = (row, idx) => string.IsNullOrEmpty(row) || row[0] == #,  //空行或者#开始的行被忽略
    Separator = \0, // 每一行的分割符,并且根据第1行的情况自动处理
    TrimData = false, // 针对每一个单元是否截去前后空格
    Comparer = null, // Can be used for case-insensitive comparison for names
    HeaderMode = HeaderMode.HeaderPresent, // 第一行设置为标题行
    ValidateColumnCount = false, // Checks each row immediately for column count
    ReturnEmptyForMissingColumn = false, // 对于无效列名,是否返回空
  //下面是针对特殊符号的处理
Aliases = null, // A collection of alternative column names AllowNewLineInEnclosedFieldValues = false, // Respects new line (either \r\n or \n) characters inside field values enclosed in double quotes. AllowBackSlashToEscapeQuote = false, // Allows the sequence "\"" to be a valid quoted value (in addition to the standard """") AllowSingleQuoteToEncloseFieldValues = false, // Allows the single-quote character to be used to enclose field values NewLine = Environment.NewLine // The new line string to use when multiline field values are read (Requires "AllowNewLineInEnclosedFieldValues" to be set to "true" for this to have any effect.) };

 

.Net开源CSV文件处理组件学习

上一篇:WarError syncing load balancer: failed to ensure load balancer: network.SubnetsClient#Get: Failure responding to request: StatusCode=403


下一篇:js 中 math的方法