[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 ...
随机推荐
- sql server 安装时提示要重启
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 打开“Session Manager”文件夹之后在右侧的区域中单 ...
- Docker+Nginx部署Angular
在部署Angular生产环境之前,需要电脑已经安装docker. 添加Dockerfile在已经完成的Angular项目的项目根目录下添加Dockerfile文件. Dockerfile文件内容: F ...
- [Android] Surface、SurfaceHolder与SurfaceView
其实相当于MVC结构的三者关系:M(Surface).V(SurfaceView).C(SurfaceHolder) 1.Surface Handle onto a raw buffer that i ...
- Git常见使用方法
图参考:http://www.ruanyifeng.com/blog/2014/06/git_remote.html 1.GitLab配置 git config --global user.name ...
- QT使用SQLite
在QT的widget中用tableview显示sqlite数据库表中的内容. 用QTcreator创建一个基于Widget类的窗口,再拖一个tableview到widget中,保存. 1.在widge ...
- VMware中四种网络连接模式的区别
安装好VMwareWorkstations之后,进行虚拟机网络配置时有四种网络连接方式,桥接.仅主机.NAT.LAN区段. 之所以有不同的模式,在我看来是为了满足不同的网络需求,总的来说:桥接.NAT ...
- Java-学习-喜欢-品牌:互联网公司成为动物园,拟人化品牌形象真的那么有意思?
ylbtech-Java-学习-喜欢-品牌:互联网公司成为动物园,拟人化品牌形象真的那么有意思? 1.返回顶部 1. 当我们在思考如何在这个碎片化.多元化的时代找到真实的.不被标签的自我时,互联网中 ...
- [UE4]制作缩略图
一.创建一个专门用来做缩略图的角色CameraCharacter,不需要实体模型. 二.Auto Possess Player设置为“Player 0” 三.重力比例改成0(这样在天上的时候就不会往下 ...
- [UE4]多播(广播)
只有服务器才有权限做广播,所以要判断确保是服务器端才做广播,有以下几种方法: 一.使用“Switch Has Authority”判断是否在服务器端 因为character一定是在服务器端创建出来的, ...
- [UE4]非常实用的插值Lerp
Alpha的数值范围是0到1. if(Alpha==0) ReturnValue=A if(Alpha==1) ReturnValue=B 如果Alpha在0到1之间,Alpha值越接近0则ReVal ...