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 读写的更多相关文章

  1. java csv - 读写及其操作.

    今天帮同学处理数据, 主要是从1w多条记录中随机获取8k条, 然后再从8k条记录中随机获取2k条记录. 最后将2k条记录中随机分成10组,使得每组的记录都不重复. 下面将我的代码都贴上来, 好以后处理 ...

  2. 使用 Apache Commons CSV 读写 CSV 文件

    有时候,我们需要读写 CSV 文件,在这里给大家分享Apache Commons CSV,读写 CSV 文件非常方便. 具体官方文档请访问Apache Commons CSV. 官方文档已经写得很详细 ...

  3. Csv读写类

    <?php /** * CSV 文件读写类 * * 示例: $header = array('name' => '名字', 'age' =>'年龄', 'idcard' => ...

  4. python 中的csv读写

    1.首先 import csv 2.读一个csv文件 data = open(filename) lines = csv.reader(data)  #reader 函数和 dirtreader函数的 ...

  5. python csv 读写操作

    import csv def read_csvList(path="./datasets/test.csv")->list: """return ...

  6. python对csv读写

    1.csv文件读取 with open("C:\\Users\\Administrator\\Desktop\\test.csv", 'r', encoding='utf-8') ...

  7. python csv读写

    https://blog.csdn.net/taotiezhengfeng/article/details/75577998

  8. C# 对CSV 读写

    下面这篇博客只介绍了简单的 用“,”隔开的方式, 不是很推荐,但是对于符合的数据类型还是挺好的 https://www.cnblogs.com/Clin/archive/2013/03/14/2959 ...

  9. python3使用csv包,读写csv文件

    python操作csv,现在很多都用pandas包了,不过python还是有一个原始的包可以直接操作csv,或者excel的,下面举个例子说明csv读写csv文件的方法: import os impo ...

随机推荐

  1. 【BZOJ 1062】 1062: [NOI2008]糖果雨 (二维树状数组)**

    1062: [NOI2008]糖果雨 Description 有一个美丽的童话:在天空的尽头有一个" 糖果国" ,这里大到摩天大厦,小到小花小草都是用糖果建造而成的.更加神奇的是, ...

  2. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  3. BZOJ 3196 Tyvj 1730 二逼平衡树 树套树 线段树 treap

    http://www.lydsy.com/JudgeOnline/problem.php?id=3196 http://hzwer.com/2734.html 线段树套treap,似乎splay也可以 ...

  4. WEB架构师成长之路 二

    法宝一:牛人爱惜自己的时间. 时间就是金钱,时间就是生命,时间如同健康一样,如果时间都没有,那成功也就是浮云了.所以牛人总是很爱惜自己的时间,总是在想办法提高自己的做事效率.我突然想了起来,我QQ里有 ...

  5. Loj10172 涂抹果酱

    题目描述 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×M 的矩形,它被划分成 N×M 个边长为 1×1 的小正方形区域(可以把蛋糕当成 NNN 行 MMM列 ...

  6. python开发_random

    和java中的random()函数一样,在python中也有类似的模块random,即随机数 下面是我做的demo 运行效果: ==================================== ...

  7. Java class 中public、protected 、friendly、private的区别

    转载自:http://hi.baidu.com/ceoct/item/7e136a2417ba6f896f2cc33c Java class 中public.protected .friendly.p ...

  8. django safe 过滤器--不对字符串进行转义(转)

    unix下的binutils短小精悍,用胶水(俺经常成管道为胶水)紧密结合在一起释放巨大的能量.django的过滤器也学习了这个方式,每个版本的django都自带了一些builtin的filter,当 ...

  9. xcode的ios工程目录结构复习

    目录结构: a.supporting files: main.m和资源文件 xxx-info.plist:包含应用程序相关属性列表,如版本,程序名等 .pch文件:预编译头文件,相当于MFC里的std ...

  10. oracle定时任务(dbms_job)

    author:skate time:2007-09-12 http://publish.it168.com/2006/0311/20060311017002.shtml 今天总结下Oracle的任务队 ...