CSV 读写
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine; public class CSVReader : IDisposable
{
private bool m_disposed;
private StreamReader m_file;
private int m_lineIndex = -;
private bool m_silent;
private int m_tokenIndex;
private string[] m_tokens;
private static char[] separators = new char[] { ',' };
private static char[] subSeparators = new char[] { '+' };
private static char[] trimCharacters = new char[] { ' ', '"' }; public CSVReader(string fileName, bool silent = false)
{
this.m_silent = silent;
try
{
this.m_file = new StreamReader(fileName);
this.m_file.ReadLine();
}
catch (Exception exception)
{
if (!this.m_silent)
{
Debug.LogWarning("Could not load: " + fileName + ", error: " + exception.Message);
}
}
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.m_disposed)
{
if (disposing && (this.m_file != null))
{
this.m_file.Dispose();
}
this.m_disposed = true;
}
} ~CSVReader()
{
this.Dispose(false);
} public bool NextRow()
{
if (this.m_file == null)
{
return false;
}
string str = this.m_file.ReadLine();
if (str == null)
{
return false;
}
this.m_tokens = str.Split(separators);
this.m_tokenIndex = ;
this.m_lineIndex++;
return true;
} private string NextToken()
{
return this.m_tokens[this.m_tokenIndex++].Trim(trimCharacters);
} public T ReadEnum<T>(T defaultValue)
{
string str = this.ReadString();
if (!string.IsNullOrEmpty(str))
{
try
{
return (T) Enum.Parse(typeof(T), str, true);
}
catch (Exception)
{
}
}
return defaultValue;
} public float ReadFloat()
{
if (this.m_tokenIndex < this.m_tokens.Length)
{
string s = this.NextToken();
float result = 0f;
if (!float.TryParse(s, out result) && !this.m_silent)
{
Debug.LogError(string.Format("Could not parse float on line {0}, token {1}: {2}", this.m_lineIndex + , this.m_tokenIndex + , s));
}
return result;
}
if (!this.m_silent)
{
Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
}
return 0f;
} public int ReadInt()
{
if (this.m_tokenIndex < this.m_tokens.Length)
{
string s = this.NextToken();
int result = ;
if (!int.TryParse(s, out result) && !this.m_silent)
{
Debug.LogError(string.Format("Could not parse int on line {0}, token {1}: {2}", this.m_lineIndex + , this.m_tokenIndex + , s));
}
return result;
}
if (!this.m_silent)
{
Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
}
return ;
} public string ReadString()
{
if (this.m_tokenIndex < this.m_tokens.Length)
{
return this.NextToken();
}
if (!this.m_silent)
{
Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
}
return string.Empty;
} public string[] ReadStringArray()
{
if (this.m_tokenIndex < this.m_tokens.Length)
{
string[] strArray = this.m_tokens[this.m_tokenIndex++].Trim(trimCharacters).Split(subSeparators);
if ((strArray.Length == ) && string.IsNullOrEmpty(strArray[]))
{
return new string[];
}
return strArray;
}
if (!this.m_silent)
{
Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
}
return new string[];
}
}
using System;
using System.IO;
using UnityEngine; public class CSVWriter : IDisposable
{
private bool m_disposed;
private StreamWriter m_file;
private bool m_startOfLine = true;
private static char separator = ',';
private static char subSeparator = '+'; public CSVWriter(string fileName, params string[] columns)
{
try
{
this.m_file = new StreamWriter(fileName);
foreach (string str in columns)
{
this.WriteString(str);
}
this.EndRow();
}
catch (Exception exception)
{
Debug.LogError("Could not open: " + fileName + ", error: " + exception.Message);
}
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.m_disposed)
{
if (disposing && (this.m_file != null))
{
this.m_file.Dispose();
}
this.m_disposed = true;
}
} public void EndRow()
{
if (this.m_file != null)
{
this.m_file.Write('\n');
this.m_startOfLine = true;
}
} ~CSVWriter()
{
this.Dispose(false);
} public void WriteFloat(float val)
{
if (!this.m_startOfLine)
{
this.m_file.Write(separator);
}
this.m_startOfLine = false;
this.m_file.Write(val);
} public void WriteInt(int val)
{
if (!this.m_startOfLine)
{
this.m_file.Write(separator);
}
this.m_startOfLine = false;
this.m_file.Write(val);
} public void WriteString(string val)
{
if (!this.m_startOfLine)
{
this.m_file.Write(separator);
}
this.m_startOfLine = false;
this.m_file.Write(val);
} public void WriteStringArray(string[] vals)
{
if (!this.m_startOfLine)
{
this.m_file.Write(separator);
}
this.m_startOfLine = false;
bool flag = true;
foreach (string str in vals)
{
if (!flag)
{
this.m_file.Write(subSeparator);
}
flag = false;
this.m_file.Write(str);
}
}
}
CSV 读写的更多相关文章
- java csv - 读写及其操作.
今天帮同学处理数据, 主要是从1w多条记录中随机获取8k条, 然后再从8k条记录中随机获取2k条记录. 最后将2k条记录中随机分成10组,使得每组的记录都不重复. 下面将我的代码都贴上来, 好以后处理 ...
- 使用 Apache Commons CSV 读写 CSV 文件
有时候,我们需要读写 CSV 文件,在这里给大家分享Apache Commons CSV,读写 CSV 文件非常方便. 具体官方文档请访问Apache Commons CSV. 官方文档已经写得很详细 ...
- Csv读写类
<?php /** * CSV 文件读写类 * * 示例: $header = array('name' => '名字', 'age' =>'年龄', 'idcard' => ...
- python 中的csv读写
1.首先 import csv 2.读一个csv文件 data = open(filename) lines = csv.reader(data) #reader 函数和 dirtreader函数的 ...
- python csv 读写操作
import csv def read_csvList(path="./datasets/test.csv")->list: """return ...
- python对csv读写
1.csv文件读取 with open("C:\\Users\\Administrator\\Desktop\\test.csv", 'r', encoding='utf-8') ...
- python csv读写
https://blog.csdn.net/taotiezhengfeng/article/details/75577998
- C# 对CSV 读写
下面这篇博客只介绍了简单的 用“,”隔开的方式, 不是很推荐,但是对于符合的数据类型还是挺好的 https://www.cnblogs.com/Clin/archive/2013/03/14/2959 ...
- python3使用csv包,读写csv文件
python操作csv,现在很多都用pandas包了,不过python还是有一个原始的包可以直接操作csv,或者excel的,下面举个例子说明csv读写csv文件的方法: import os impo ...
随机推荐
- 全景图从拍摄到 online
全景图从拍摄到 online Panorama, CSS3, Canvas, 3D 2015-11-04 拍摄设备 照片拼接 制作 3D Cube Demo 拍摄设备 电动自动全景云台 Gigapan ...
- [BZOj4336][BJOI2015]骑士的旅行(树链剖分+线段树)
树链剖分,对每个叶子用multiset记录前K大士兵,其余节点通过从儿子归并维护前K大士兵.过于模板. #include<set> #include<cstdio> #incl ...
- 20162328WJH实验五网络编程与安全实验报告
20162328WJH实验五网络编程与安全实验报告 一.实验五 网络编程与安全-1 结对实现中缀表达式转后缀表达式的功能 MyBC.java 结对实现从上面功能中获取的表达式中实现后缀表达式求值的功能 ...
- 初涉springboot(二)
概述 1.环境说明 2.HelloWorld项目细节 3.小结 一. 关于环境补充 在上一篇的HellWorld项目中,我们构建SpringBoot项目,采用的是jdk1.8版本,springbo ...
- HDU 5650 so easy 数学
so easy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5650 Description Given an array with n integ ...
- BZOJ 4027: [HEOI2015]兔子与樱花 树上dp
4027: [HEOI2015]兔子与樱花 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...
- CentOS6 下编译安装 MySQL 5.6.26
CentOS6下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.26. 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | ...
- 通过手机音频口,实现与单片机通讯,做电子签名成功n
手机端的Ukey便携产品, 可以管理证书.加密解密.电子签名 : 1.通讯稳定,生成签名成功率100% 2.证书固化,私钥安全 3.走手机音频接口,通用.跨平台 4.耗电少,自带电池可长期供电,且可充 ...
- Added components improve switching-regulator stability
Added components improve switching-regulator stability
- Spring MapFactoryBean例子
MapFactoryBean类为开发者提供了一种在Spring的bean配置文件中创建一个具体的Map集合类(HashMap和TreeMap). 这里有一个MapFactoryBean.例如,在运行时 ...