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 ...
随机推荐
- 【BZOJ 4555】 4555: [Tjoi2016&Heoi2016]求和 (NTT)
4555: [Tjoi2016&Heoi2016]求和 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 315 Solved: 252 Des ...
- CentOS 报错cannot execute binary file
在安装软件过程中执行文件,报错cannot execute binary file 1.查看是否root用户登录,当前用户是否有可执行权限 2.ls -l 查看文件是否具有可执行权限 3.要使用对应的 ...
- @SuppressWarning 抑制警告注解
@SuppressWarning 抑制警告注解 Java.lang.SuppressWarnings 是 J2SE5.0中标准的Annotation 之一. 可以标注在类,字段,方法,参数,构造方法, ...
- What is the Linux High Availabi
What is the Linux High Availabi 简介: 高可用性群集的出现是为了使群集的整体服务尽可能可用,以便考虑计算硬件和软件的易错性.如果高可用性群集中的主节点发生 ...
- How to run WPF – XBAP as Full Trust Application
Recently I work on WPF-XBAP application that will run from intranet website: This application must h ...
- Unity3d面试6
1,如何避免点击UI按钮时穿透,同时触发了相同位置场景模型的点击事件的情况?(NGUI)1,如何避免点击UI按钮时穿透,同时触发了相同位置场景模型的点击事件的情况?(NGUI 判断 是否点击到UI) ...
- Ant:Ant 入门
背景 自从有了 Maven 以后,Ant 视乎就不流行了,不过 Ant 还是有其应用场景的,Ant 的思想比较简洁,如下: 一个 project 包含多个 target(类似成员方法). 一个 tar ...
- Jacob的使用出错总结
转自:http://blog.163.com/wm_at163/blog/static/13217349020114166447941/ Jacob的使用方法: 1.在工程中导入 jacob.jar ...
- 获取easyui calendar 属性
<div id="cc" class="easyui-calendar" style="width:250px;height:250px;&qu ...
- 常见C++内存池技术
原文:http://www.cppblog.com/weiym/archive/2013/04/08/199238.html 总结下常见的C++内存池,以备以后查询.应该说没有一个内存池适合所有的情况 ...