[Unity工具]CSV工具类
参考链接:
https://www.cnblogs.com/lulianqi/p/6385503.html
http://blog.csdn.net/paul342/article/details/22800137
说明:
1.写入一个单元格时,如果含有逗号,则需要将整个字段用双引号括起来;如果里面还有双引号就替换成两个双引号。
2.写入一行时,末尾要加上\r\n作为行分隔符。
3.读取时,也要根据上面的写入规则进行解析。
代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text; public class CSVTool { private static char _csvSeparator = ',';
private static bool _trimColumns = false; //获取一个单元格的写入格式
public static string GetCSVFormat(string str)
{
string tempStr = str;
if (str.Contains(","))
{
if (str.Contains("\""))
{
tempStr = str.Replace("\"", "\"\"");
}
tempStr = "\"" + tempStr + "\"";
}
return tempStr;
} //获取一行的写入格式
public static string GetCSVFormatLine(List<string> strList)
{
string tempStr = "";
for (int i = ; i < strList.Count - ; i++)
{
string str = strList[i];
tempStr = tempStr + GetCSVFormat(str) + ",";
}
tempStr = tempStr + GetCSVFormat(strList[strList.Count - ]) + "\r\n";
return tempStr;
} //解析一行
public static List<string> ParseLine(string line)
{
StringBuilder _columnBuilder = new StringBuilder();
List<string> Fields = new List<string>();
bool inColumn = false; //是否是在一个列元素里
bool inQuotes = false; //是否需要转义
bool isNotEnd = false; //读取完毕未结束转义
_columnBuilder.Remove(, _columnBuilder.Length); //空行也是一个空元素,一个逗号是2个空元素
if (line == "")
{
Fields.Add("");
} // Iterate through every character in the line
for (int i = ; i < line.Length; i++)
{
char character = line[i]; // If we are not currently inside a column
if (!inColumn)
{
// If the current character is a double quote then the column value is contained within
// double quotes, otherwise append the next character
inColumn = true;
if (character == '"')
{
inQuotes = true;
continue;
} } // If we are in between double quotes
if (inQuotes)
{
if ((i + ) == line.Length)//这个字符已经结束了整行
{
if (character == '"') //正常转义结束,且该行已经结束
{
inQuotes = false;
continue; //当前字符不用添加,跳出后直结束后会添加该元素
}
else //异常结束,转义未收尾
{
isNotEnd = true;
}
}
else if (character == '"' && line[i + ] == _csvSeparator) //结束转义,且后面有可能还有数据
{
inQuotes = false;
inColumn = false;
i++; //跳过下一个字符
}
else if (character == '"' && line[i + ] == '"') //双引号转义
{
i++; //跳过下一个字符
}
else if (character == '"') //双引号单独出现(这种情况实际上已经是格式错误,为了兼容可暂时不处理)
{
throw new Exception("格式错误,错误的双引号转义");
}
//其他情况直接跳出,后面正常添加 }
else if (character == _csvSeparator)
inColumn = false; // If we are no longer in the column clear the builder and add the columns to the list
if (!inColumn) //结束该元素时inColumn置为false,并且不处理当前字符,直接进行Add
{
Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
_columnBuilder.Remove(, _columnBuilder.Length); }
else // append the current column
_columnBuilder.Append(character);
} // If we are still inside a column add a new one (标准格式一行结尾不需要逗号结尾,而上面for是遇到逗号才添加的,为了兼容最后还要添加一次)
if (inColumn)
{
if (isNotEnd)
{
_columnBuilder.Append("\r\n");
}
Fields.Add(_trimColumns ? _columnBuilder.ToString().Trim() : _columnBuilder.ToString());
}
else //如果inColumn为false,说明已经添加,因为最后一个字符为分隔符,所以后面要加上一个空元素
{
Fields.Add("");
} return Fields;
} //读取文件
public static List<List<string>> Read(string filePath, Encoding encoding)
{
List<List<string>> result = new List<List<string>>();
string content = File.ReadAllText(filePath, encoding);
string[] lines = content.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = ; i < lines.Length; i++)
{
List<string> line = ParseLine(lines[i]);
result.Add(line);
}
return result;
} //写入文件
public static void Write(string filePath, Encoding encoding, List<List<string>> result)
{
StringBuilder builder = new StringBuilder();
for (int i = ; i < result.Count; i++)
{
List<string> line = result[i];
builder.Append(GetCSVFormatLine(line));
}
File.WriteAllText(filePath, builder.ToString(), encoding);
} //打印
public static void Debug(List<List<string>> result)
{
for (int i = ; i < result.Count; i++)
{
List<string> line = result[i];
for (int j = ; j < line.Count; j++)
{
UnityEngine.Debug.LogWarning(line[j]);
}
}
}
}
测试:


[Unity工具]CSV工具类的更多相关文章
- CSV工具类
分享自己昨天写的CSV工具类, 主要实现解析CSV格式, 直接上代码 #region private /// <summary> /// 从sr当前位置解析一个栏位 /// </su ...
- Unity自动打包工具
转载 https://blog.csdn.net/ynnmnm/article/details/36774715 最开始有写打包工具的想法,是因为看到<啪啪三国>王伟峰分享的一张图,他们有 ...
- 多测师讲解常用的测试工具分为10类_高级讲师肖sir
我们将常用的测试工具分为10类. 1. 测试管理工具 2. 接口测试工具 3. 性能测试工具 4. C/S自动化工具 5.白盒测试工具 6.代码扫描工具 7.持续集成工具 8.网络测试工具 9.app ...
- 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实
2.使用工厂方法模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...
- 1、使用简单工厂模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程
1.使用简单工厂模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...
- Atitit s2018.5 s5 doc list on com pc.docx Acc 112237553.docx Acc baidu netdisk.docx Acc csdn 18821766710 attilax main num.docx Atiitt put post 工具 开发工具dev tool test.docx Atiitt 腾讯图像分类相册管家.docx
Atitit s2018.5 s5 doc list on com pc.docx Acc 112237553.docx Acc baidu netdisk.docx Acc csdn 1882 ...
- 在ArcEngine中使用Geoprocessing工具-执行工具
转自原文在ArcEngine中使用Geoprocessing工具-执行工具 来解析一下Geoprocessor类的Execute方法,他有两种重载,Execute(IGPProcess, ITrack ...
- Unity C# CSV文件解析与加载(已更新移动端处理方式)
在游戏开发过程中,经常要用到Excel编辑各类数据,如果可以直接用Excel支持的文件格式来读取数据,修改将非常便捷. Excel支持导出CSV类型的文件,这类文件不仅可以用Excel直接打开修改,即 ...
- C#操作CSV存取类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
随机推荐
- 【java】Hello World
class Demo { //程序的主入口,能保证类的独立运行 public static void main(String[] args) { System.out.println("He ...
- Centos 使用find查找
CentOS查找目录或文件 find / -name svn 查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) -name 查找关键字 ...
- mysql 闪回原理
利用MySQL闪回技术恢复误删除误更改的数据 笔者相信很多人都遇到过忘带where条件或者where条件漏写了一个和写错了的情况,结果执行了delete/update后把整张表的数据都给改了.传统的解 ...
- DOM 讲解
DOM,全称documention,文档意思 ,就是把整个html文档当成一个对象来操作,里面有很多方法,如getElementByid(),getElementByid().innerText(); ...
- Android app内存限制OOM
Android系统的每个app做了内存限制,版本的android系统,不同的设备对每个app的内存限制可能有所不同,从早期的16M ,32M到现在的256M,384M...虽然内存增大了,但是不代表就 ...
- c#读取Sybase中文乱码的解决办法
最近需要从Sybase数据库中获取数据.用Sybase.Data.AseClient连接的话比较简单,但中文数据会有乱码.用Sybase自带的工具SQL Advantage设置好编码和语言,是可以正常 ...
- g++编译后中文显示乱码解决方案(c++)
g++编译后中文显示乱码解决方案 环境:Windows 10 专业版 GCC版本:5.3.0 测试代码: 1 #include <iostream> 2 using namespace ...
- 基于scroll的吸顶效果
本次要实现的是一种常见的网页效果,如下: 页面由头部,导航,主体内容三部分组成,当页面发生滚动时,头部逐渐隐藏,导航部分向上移动,直到导航部分距离浏览器顶部为零时,导航部分固定不动,保持吸顶效果,如下 ...
- Apollo分布式配置中心部署以及使用
一.简介Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场 ...
- 详解MySQL主从复制实战 - 基于GTID的复制
基于GTID的复制 简介 基于GTID的复制是MySQL 5.6后新增的复制方式. GTID (global transaction identifier) 即全局事务ID, 保证了在每个在主库上提交 ...