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 ...
随机推荐
- Linux下C语言多文件的编译以及makefile的应用
1.关于编译和链接 一般来说,无论是C.C++,首先要把源文件编译成中间代码文件,在Windows下也就是.obj文件,UNIX下是.o文件,即Object File,这个动作叫做编译(compile ...
- Codeforces Beta Round #7 C. Line Exgcd
C. Line 题目连接: http://www.codeforces.com/contest/7/problem/C Description A line on the plane is descr ...
- VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) B. Chat Order 水题
B. Chat Order 题目连接: http://www.codeforces.com/contest/637/problem/B Description Polycarp is a big lo ...
- SqlServer Base64解码中文
最近在做一个和拥有TurboCRM相关的项目,其中需要取出客户信息的联系人字段,经过查看,这个字段在存入时用Base64加密过了 这个功能在应用层实现是很方便的,但是由于一些特殊原因,只能放到SqlS ...
- [原]Redis使用场景及使用经验
Redis is an open source (BSD licensed), in-memory data structure store! 欢迎转载,转载请注明出处 刚刚结束一个游戏类的活动项目, ...
- spring aop 理解
aop简介 aop是spring 的两大特性之一,还有IOC.主要提供面向切面的编程思想,区分于面向对象编程. aop原理(动态代理+反射) 在一个方法体中,可能会存在很多其他的方法调用,我们可以把每 ...
- 破解MyEclipse2015 stable3.0(亲测可用)
整个破解过程最好断网: 1.安装好MyEclipse2015 stable3后,打开设置好工作目录后,退出.2.将plugins文件夹中的文件拷贝到myeclipse安装目录的plugins文件夹下, ...
- 典型案例收集-OpenVPN不同网段的访问控制(转)
一.案例1 针对不同的客户端指定不同的等级和权限. 通常的方法是: 1.每个客户端分配不同的IP地址: 2.利用防火墙对不同的IP地址进行控制: 例如: 1.公司内部网段是10.66.4.0/24: ...
- Win8系统108个运行命令 你能记住多少?
Win8运行命令:程序和功能 取消了开始菜单的Win8让人感觉很不习惯,这才发现原来开始菜单可以做这么多事.不过Win8中的一些快捷键还沿用了Windows一直以来的习惯,比如按下Windows ...
- java string(2)
1. indexOf()函数是一个执行速度非常快的函数,可以用其与subString()实现高效的字符串分割,比内置的要高效. 2. charAt()方法也是高效率的函数,可以用其实现高效的start ...