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 ...
随机推荐
- [NOIP2017]列队(线段树/裂点splay)
考虑n=1的做法,就是支持: 1.在线删一个数 2.在结尾加一个数 3.查询序列的第y个数 用线段树记录区间内被删元素的个数,可以通过线段树上二分快速得解,对于新增的数,用vector记录即可. 对于 ...
- [COGS2426][HZOI 2016]几何
[COGS2426][HZOI 2016]几何 题目大意: 给定平面坐标系内\(n\)个整点,求这些整点能构成的正多边形的边数的最大值. 思路: 一个基本结论:平面直角坐标系内能够形成的正多边形一定是 ...
- 注解@Aspect实现AOP功能
springboot中pom引入jar <!-- aop 切面 --> <dependency> <groupId>org.springframework.boot ...
- Mac下配置Idea的Maven
环境版本: Mac OS: 10.13.4 JDK: 1.8 Idea: 2018.3 Maven: 3.6.0 Maven 相关配置: Maven 下载: http://maven.apache.o ...
- Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- sybase数据库技术 :游标可更新与for read only/for update
在定义游标时不指定for update 或 for read only,ASE会检查以了解游标是否可更新: 如果游标查询语句中包含order by子句,则ASE会将游标定义为只读:其它情况下定义为可更 ...
- iOS开发 跳转场景的三种方式
iOS开发 跳转场景的三种方式 2012年10月17日, 15:32 假设A跳转到B,三种方法:1.按住ctrl键,拖动A上的控件(比如说UIButton)到B上,弹出菜单,选择Modal.不需要写任 ...
- Ubuntu环境下配置GCC
Ubuntu网络环境下安装GCC及其头文件步骤: 1.Ubuntu环境下配置GCC 刚装好的GCC什么都不能编译,因为没有一些必须的头文件,所以要安装build-essential,安装了这个包会安装 ...
- android 各种颜色值 colors.xml
<?xml version="1.0" encoding="utf-8" ?> <resources> <color name=& ...
- python接口自动化8-参数化
前言 前面一篇实现了参数的关联,那种只是记流水账的完成功能,不便于维护,也没什么可读性,接下来这篇可以把每一个动作写成一个函数,这样更方便了. 参数化的思维只需记住一点:不要写死 (由于博客园登录机制 ...