CSV全称是Comma-Separated Values(逗号分隔值)。作为一种数据传输与存储的格式,它显然没有xml,json强大,只能进行一些二维数组的数据处理,但它在项目还是经常会用到.

CSV的字符规则:

1 开头是不留空,以行为单位。

2 可含或不含列名,含列名则居文件第一行。

3 一行数据不跨行,无空行。

4 以半角逗号(即,)作分隔符,列为空也要表达其存在。

5 列内容如存在半角逗号(即,)换行符\r(10)\n(13)则用半角引号(即"")将该字段值包含起来。

6 列内容如存在半角引号(即")则应替换成半角双引号("")转义,并用半角引号(即"")将该字段值包含起来。

7 文件读写时引号,逗号操作规则互逆。

8 内码格式不限,可为 ASCII、Unicode 或者其他。

9 数据结束外,有\r\n作为结束标记

说明:这个规则对于excel完全适合

算法实现

namespace CSV
{
/// <summary>
/// CSVUtil 用来处理CSV格式的文件内容成一二维数组。
/// </summary>
public class CSVUtil
{
private string content;
public CSVUtil()
{ }
public CSVUtil(string _content)
{
content = _content;
}
public CSVUtil(string _content, bool _hasHeader)
{
content = _content;
hasHeader = _hasHeader;
} private bool hasHeader = true;
public bool HasHeader
{
get { return hasHeader; }
set { hasHeader = value; }
} private string[] headr;
public string[] Header
{
get { return headr; }
} private string[][] data;
public string[][] Data
{
get { return data; }
} /// <summary> /// 分割 CVS 文件内容为一个二维数组。 /// </summary> /// <param name="src">CVS 文件内容字符串</param> /// <returns>二维数组。String[line count][column count]</returns> public void Parse()
{ // 如果输入为空,返回 0 长度字符串数组 if (content == null || content.Length == 0) return;
string st = "";
List<List<string>> lines = new List<List<string>>(); // 行集合。其元素为行
List<string> cells = new List<string>(); // 单元格集合。其元素为一个单元格
bool beginWithQuote = false;
int maxColumns = 0;
// 遍历字符串的字符
for (int i = 0; i < content.Length; i++)
{
char ch = content[i];
#region CR 或者 LF
//A record separator may consist of a line feed (ASCII/LF=0x0A),
//or a carriage return and line feed pair (ASCII/CRLF=0x0D 0x0A).
// 这里我不明白CR为什么不作为separator呢,在Mac OS上好像是用CR的吧。
// 这里我“容错”一下,CRLF、LFCR、CR、LF都作为separator if (ch == '\r')
{
#region CR
if (beginWithQuote)
{
st += ch;
}
else
{
if (i + 1 < content.Length && content[i + 1] == '\n')
{ // 如果紧接的是LF,那么直接把LF吃掉
i++;
} //line = new String[cells.Count];
//System.Array.Copy (cells.ToArray(typeof(String)), line, line.Length);
//lines.Add(line); // 把上一行放到行集合中去 cells.Add(st);
st = "";
beginWithQuote = false;
maxColumns = (cells.Count > maxColumns ? cells.Count : maxColumns);
lines.Add(cells);
st = "";
cells = new List<string>();
}
#endregion CR
} else if (ch == '\n')
{
#region LF
if (beginWithQuote)
{
st += ch;
}
else
{
if (i + 1 < content.Length && content[i + 1] == '\r')
{ // 如果紧接的是LF,那么直接把LF吃掉
i++;
}
//line = new String[cells.Count];
//System.Array.Copy (cells.ToArray(typeof(String)), line, line.Length);
//lines.Add(line); // 把上一行放到行集合中去 cells.Add(st);
st = "";
beginWithQuote = false;
maxColumns = (cells.Count > maxColumns ? cells.Count : maxColumns);
lines.Add(cells);
st = "";
cells = new List<string>(); }
#endregion LF
} #endregion CR 或者 LF
else if (ch == '\"')
{ // 双引号
#region 双引号 if (beginWithQuote)
{
i++;
if (i >= content.Length)
{
cells.Add(st);
st = "";
beginWithQuote = false;
}
else
{
ch = content[i];
if (ch == '\"')
{
st += ch;
}
else if (ch == ',')
{
cells.Add(st);
st = "";
beginWithQuote = false;
}
else
{
throw new Exception("Single double-quote char mustnt exist in filed " + (cells.Count + 1) + " while it is begined with quote\nchar at:" + i);
}
}
} else if (st.Length == 0)
{
beginWithQuote = true;
} else
{
throw new Exception("Quote cannot exist in a filed which doesnt begin with quote!\nfield:" + (cells.Count + 1));
}
#endregion 双引号
} else if (ch == ',')
{
#region 逗号
if (beginWithQuote)
{
st += ch;
}
else
{
cells.Add(st);
st = "";
beginWithQuote = false;
}
#endregion 逗号
} else
{
#region 其它字符
st += ch;
#endregion 其它字符
} } if (st.Length != 0)
{
if (beginWithQuote)
{
throw new Exception("last field is begin with but not end with double quote");
}
else
{
cells.Add(st);
maxColumns = (cells.Count > maxColumns ? cells.Count : maxColumns);
lines.Add(cells);
}
}
int dataRowCount = hasHeader ? lines.Count - 1 : lines.Count;
data = new string[dataRowCount][];
for (int i = 0; i < lines.Count; i++)
{
cells = (List<string>)lines[i];
try
{
if (hasHeader == true && i == 0)
{
headr = new string[maxColumns];
for (int j = 0; j < maxColumns; j++)
{
headr[j] = cells[j];
}
}
else
{
int dataIndex = hasHeader ? i - 1 : i;
data[dataIndex] = new string[maxColumns];
for (int j = 0; j < maxColumns; j++)
{
data[dataIndex][j] = cells[j];
}
}
}
catch (Exception ex)
{ throw new Exception(ex.Message + "\nfield:" + (i + 1));
}
}
//System.Array.Copy(lines.ToArray(typeof(String[])), ret, ret.Length);
return; } public static string FormatField(object obj)
{
string result = string.Empty;
if (obj != null)
{
string old = obj.ToString(); if (old.IndexOf('\"') > -1 || old.IndexOf(',') > -1 || old.IndexOf('\n') > -1 || old.IndexOf('\r') > -1)
{
result = "\"" + old.Replace("\"", "\"\"") + "\"";
}
else
{
result = old;
}
}
return result;
} public static string FormatList<T>(IEnumerable<T> source, List<string> outputPropertys)
{
StringBuilder sbResult = new StringBuilder();
Dictionary<string, MethodInfo> methods = new Dictionary<string, MethodInfo>();
object val = null;
foreach (string propertyName in outputPropertys)
{
PropertyInfo p = typeof(T).GetProperty(propertyName);
methods.Add(propertyName, p.GetGetMethod());
sbResult.Append(propertyName + ",");
}
sbResult.Remove(sbResult.Length - 1, 1);
sbResult.Append(Environment.NewLine);
foreach (T item in source)
{
foreach (KeyValuePair<string, MethodInfo> method in methods)
{
val = method.Value.Invoke(item, null);
sbResult.Append(FormatField(val) + ",");
}
sbResult.Remove(sbResult.Length - 1, 1);
sbResult.Append(Environment.NewLine);
}
return sbResult.ToString();
}
}
}

细说CSV的更多相关文章

  1. csvkit---python一个牛逼到不行的csv处理库

    先吐槽一下:不管是百度还是谷歌,查来查去除了官方文档之外就没有任何可以借鉴的例子,虽然官方文档写的挺好的.但是我一直以为是在python语言的方式运行的,结果是以命令行的方式运行的,搞得我还以为这个库 ...

  2. PHP生成器细说

    之前写过关于生成器的文章,可能还不够详细,正好群里有朋友在讨论.觉得还是有必要再细说下,如果大家做过Python或者其他语言的,对于生成器应该不陌生.生成器是PHP 5.5.才引入的功能,也许大家觉得 ...

  3. [转]RPA认证 Developer UIPath Certificate,细说uipath认证学习,Online Quiz和Practical Exam项目详解

    本文转自:https://blog.csdn.net/u010369735/article/details/88621195 UIPath,RPA里算是比较简单易操作的一款软件了,因为公司业务的需要, ...

  4. 匹夫细说C#:庖丁解牛迭代器,那些藏在幕后的秘密

    0x00 前言 在匹夫的上一篇文章<匹夫细说C#:不是“栈类型”的值类型,从生命周期聊存储位置>的最后,匹夫以总结和后记的方式涉及到一部分迭代器的知识.但是觉得还是不够过瘾,很多需要说清楚 ...

  5. 细说WebSocket - Node篇

    在上一篇提高到了 web 通信的各种方式,包括 轮询.长连接 以及各种 HTML5 中提到的手段.本文将详细描述 WebSocket协议 在 web通讯 中的实现. 一.WebSocket 协议 1. ...

  6. mysql 大表拆分成csv导出

    最近公司有一个几千万行的大表需要按照城市的id字段拆分成不同的csv文件. 写了一个自动化的shell脚本 在/home/hdh 下面 linux-xud0:/home/hdh # lltotal 1 ...

  7. Bulk Insert:将文本数据(csv和txt)导入到数据库中

    将文本数据导入到数据库中的方法有很多,将文本格式(csv和txt)导入到SQL Server中,bulk insert是最简单的实现方法 1,bulk insert命令,经过简化如下 BULK INS ...

  8. 匹夫细说C#:委托的简化语法,聊聊匿名方法和闭包

    0x00 前言 通过上一篇博客<匹夫细说C#:庖丁解牛聊委托,那些编译器藏的和U3D给的>的内容,我们实现了使用委托来构建我们自己的消息系统的过程.但是在日常的开发中,仍然有很多开发者因为 ...

  9. 细说Java主流日志工具库

    概述 在项目开发中,为了跟踪代码的运行情况,常常要使用日志来记录信息. 在Java世界,有很多的日志工具库来实现日志功能,避免了我们重复造轮子. 我们先来逐一了解一下主流日志工具. java.util ...

随机推荐

  1. 1、TensorFlow简介

    参考:http://www.tensorfly.cn/tfdoc/get_started/basic_usage.html 1.用TensorFlow构造一个简单的线性拟合: # -*- coding ...

  2. qt线程睡眠

    头文件 #include <QThread> 接口函数: void QThread::sleep ( unsigned long secs )   [static protected] v ...

  3. N1 Armbian 安装 Domoticz

    前言 N1 中安装 Domoticz 的方法与这篇类似,MQTT 服务器改用 mosquitto,更轻量级. 步骤 安装 Domoticz,只选择 HTTP 8080 端口 curl -sSL ins ...

  4. python学习,day1:循环判断基本语句的几个代码

    # coding=utf-8 # Author: RyAn Bi count = 0 '''while True : print('count:',count) count = count + 1 i ...

  5. POJ1475 Pushing Boxes 华丽丽的双重BFS

    woc累死了写了两个半小时...就是BFS?我太菜了... 刚开始以为让人预先跑一遍BFS,然后一会儿取两节加起来就好了,结果发现求出来的最短路(就是这个意思)会因箱子的移动而变化....我死了QWQ ...

  6. HDU - 1525 博弈 暴力分析

    先来看看比较显然的几个局面 (a,0) 先手必败 (a,a) 先手必胜 (a,ak) 先手必胜 (a,ak+r),k>1 先手必胜,因为先手有主动权把(a,r)让给后手或留给自己 对于开局(a, ...

  7. 计算hashCode通用计算公式

    1.java计算公式 @Override public int hashCode() { //设置初始值 ; //假设有效域为: name,age,idCardNo,incomeAnnual,sex, ...

  8. v-model 用在组件中

    官方文档: 使用自定义事件的表单输入组件 官方也说明了,v-model只不过是一个语法糖而已,真正的实现靠的还是 1. v-bind : 绑定响应式数据 2. 触发 input 事件 并传递数据 (核 ...

  9. Android耗时操作

    No subscribers registered for event class com.test.MessageEvent import de.greenrobot.event.EventBus; ...

  10. switch case 注意事项+1 及 case合并综合练习例子

    case可以合并: 练习11:根据输入的星期,得到具体每天做的事情.星期一学习,星期二学习,星期三自习,星期四学习,星期五自习,星期六学习,星期日学习 class Switch02{ public s ...