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 ...
随机推荐
- 洛谷P1149 火柴棒等式
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1.加号与等号 ...
- [BZOJ4338][BJOI2015]糖果(扩展Lucas)
先求出式子$P_{C_{K+m-1}^{m}}^{n}$,然后对于排列直接$O(n)$求解,对于组合用扩展Lucas求解. 但这题数据并没有保证任何一个模数的质因子的$p^k$在可线性处理的范围内,于 ...
- 【插头DP】BZOJ1814-Formula
[题目大意] 给出一个m*n的矩阵里面有一些格子为障碍物,求经过所有非障碍格子的哈密顿回路个数. [思路] 最典型的插头DP.分为三种情况: (1)当前格子既没有上插头也没有左插头. 如果下边和右边都 ...
- 【最小割】BZOJ2039- [2009国家集训队]employ人员雇佣
[题目大意] 给定n个人,每个人有一个佣金,i和j如果同时被雇佣会产生2*E(i,j)的效益,i和j如果一个被雇佣一个不被雇佣会产生E(i,j)的亏损,求最大收益. [思路] 如果没有亏损,其实非常类 ...
- [MVC4]Data Annotations Extensions:无法使用EmailAddress等验证特性的解决方法
本文地址:http://www.cnblogs.com/egger/p/3404159.html 欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 数据注解(Data Annotations) Web应用 ...
- SpringMvc的服务器端跳转和客户端跳转
首先,找到 package org.springframework.web.servlet.view; public class InternalResourceViewResolver extend ...
- 字符串转base64,base64转字符串
[JavaScript原生提供两个Base64相关方法] btoa():字符串或二进制值转为Base64编码 atob():Base64编码转为原来的编码 备注:利用这两个原生方法,我们来封装一下,标 ...
- linux文件系统命令(6)---touch和mkdir
一.目的 本文将介绍linux下新建文件或文件夹.删除文件或文件夹命令. touch能够新建文件,mkdir用来新建文件夹.rm用来删除文件或文件夹. 本文将选取ubu ...
- mmap函数使用
UNIX网络编程第二卷进程间通信对mmap函数进行了说明.该函数主要用途有三个:1.将一个普通文件映射到内存中,通常在需要对文件进行频繁读写时使用,这样用内存读写取代I/O读写,以获得较高的性能:2. ...
- linux查看端口被哪个服务占用的命令
netstat -tunpl | grep 6379