1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5.  
  6. public class CSVReader : IDisposable
  7. {
  8. private bool m_disposed;
  9. private StreamReader m_file;
  10. private int m_lineIndex = -;
  11. private bool m_silent;
  12. private int m_tokenIndex;
  13. private string[] m_tokens;
  14. private static char[] separators = new char[] { ',' };
  15. private static char[] subSeparators = new char[] { '+' };
  16. private static char[] trimCharacters = new char[] { ' ', '"' };
  17.  
  18. public CSVReader(string fileName, bool silent = false)
  19. {
  20. this.m_silent = silent;
  21. try
  22. {
  23. this.m_file = new StreamReader(fileName);
  24. this.m_file.ReadLine();
  25. }
  26. catch (Exception exception)
  27. {
  28. if (!this.m_silent)
  29. {
  30. Debug.LogWarning("Could not load: " + fileName + ", error: " + exception.Message);
  31. }
  32. }
  33. }
  34.  
  35. public void Dispose()
  36. {
  37. this.Dispose(true);
  38. GC.SuppressFinalize(this);
  39. }
  40.  
  41. protected virtual void Dispose(bool disposing)
  42. {
  43. if (!this.m_disposed)
  44. {
  45. if (disposing && (this.m_file != null))
  46. {
  47. this.m_file.Dispose();
  48. }
  49. this.m_disposed = true;
  50. }
  51. }
  52.  
  53. ~CSVReader()
  54. {
  55. this.Dispose(false);
  56. }
  57.  
  58. public bool NextRow()
  59. {
  60. if (this.m_file == null)
  61. {
  62. return false;
  63. }
  64. string str = this.m_file.ReadLine();
  65. if (str == null)
  66. {
  67. return false;
  68. }
  69. this.m_tokens = str.Split(separators);
  70. this.m_tokenIndex = ;
  71. this.m_lineIndex++;
  72. return true;
  73. }
  74.  
  75. private string NextToken()
  76. {
  77. return this.m_tokens[this.m_tokenIndex++].Trim(trimCharacters);
  78. }
  79.  
  80. public T ReadEnum<T>(T defaultValue)
  81. {
  82. string str = this.ReadString();
  83. if (!string.IsNullOrEmpty(str))
  84. {
  85. try
  86. {
  87. return (T) Enum.Parse(typeof(T), str, true);
  88. }
  89. catch (Exception)
  90. {
  91. }
  92. }
  93. return defaultValue;
  94. }
  95.  
  96. public float ReadFloat()
  97. {
  98. if (this.m_tokenIndex < this.m_tokens.Length)
  99. {
  100. string s = this.NextToken();
  101. float result = 0f;
  102. if (!float.TryParse(s, out result) && !this.m_silent)
  103. {
  104. Debug.LogError(string.Format("Could not parse float on line {0}, token {1}: {2}", this.m_lineIndex + , this.m_tokenIndex + , s));
  105. }
  106. return result;
  107. }
  108. if (!this.m_silent)
  109. {
  110. Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
  111. }
  112. return 0f;
  113. }
  114.  
  115. public int ReadInt()
  116. {
  117. if (this.m_tokenIndex < this.m_tokens.Length)
  118. {
  119. string s = this.NextToken();
  120. int result = ;
  121. if (!int.TryParse(s, out result) && !this.m_silent)
  122. {
  123. Debug.LogError(string.Format("Could not parse int on line {0}, token {1}: {2}", this.m_lineIndex + , this.m_tokenIndex + , s));
  124. }
  125. return result;
  126. }
  127. if (!this.m_silent)
  128. {
  129. Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
  130. }
  131. return ;
  132. }
  133.  
  134. public string ReadString()
  135. {
  136. if (this.m_tokenIndex < this.m_tokens.Length)
  137. {
  138. return this.NextToken();
  139. }
  140. if (!this.m_silent)
  141. {
  142. Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
  143. }
  144. return string.Empty;
  145. }
  146.  
  147. public string[] ReadStringArray()
  148. {
  149. if (this.m_tokenIndex < this.m_tokens.Length)
  150. {
  151. string[] strArray = this.m_tokens[this.m_tokenIndex++].Trim(trimCharacters).Split(subSeparators);
  152. if ((strArray.Length == ) && string.IsNullOrEmpty(strArray[]))
  153. {
  154. return new string[];
  155. }
  156. return strArray;
  157. }
  158. if (!this.m_silent)
  159. {
  160. Debug.LogError(string.Format("Out of tokens on line {0}, requested token at index {1}", this.m_lineIndex + , this.m_tokenIndex + ));
  161. }
  162. return new string[];
  163. }
  164. }
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. public class CSVWriter : IDisposable
  6. {
  7. private bool m_disposed;
  8. private StreamWriter m_file;
  9. private bool m_startOfLine = true;
  10. private static char separator = ',';
  11. private static char subSeparator = '+';
  12.  
  13. public CSVWriter(string fileName, params string[] columns)
  14. {
  15. try
  16. {
  17. this.m_file = new StreamWriter(fileName);
  18. foreach (string str in columns)
  19. {
  20. this.WriteString(str);
  21. }
  22. this.EndRow();
  23. }
  24. catch (Exception exception)
  25. {
  26. Debug.LogError("Could not open: " + fileName + ", error: " + exception.Message);
  27. }
  28. }
  29.  
  30. public void Dispose()
  31. {
  32. this.Dispose(true);
  33. GC.SuppressFinalize(this);
  34. }
  35.  
  36. protected virtual void Dispose(bool disposing)
  37. {
  38. if (!this.m_disposed)
  39. {
  40. if (disposing && (this.m_file != null))
  41. {
  42. this.m_file.Dispose();
  43. }
  44. this.m_disposed = true;
  45. }
  46. }
  47.  
  48. public void EndRow()
  49. {
  50. if (this.m_file != null)
  51. {
  52. this.m_file.Write('\n');
  53. this.m_startOfLine = true;
  54. }
  55. }
  56.  
  57. ~CSVWriter()
  58. {
  59. this.Dispose(false);
  60. }
  61.  
  62. public void WriteFloat(float val)
  63. {
  64. if (!this.m_startOfLine)
  65. {
  66. this.m_file.Write(separator);
  67. }
  68. this.m_startOfLine = false;
  69. this.m_file.Write(val);
  70. }
  71.  
  72. public void WriteInt(int val)
  73. {
  74. if (!this.m_startOfLine)
  75. {
  76. this.m_file.Write(separator);
  77. }
  78. this.m_startOfLine = false;
  79. this.m_file.Write(val);
  80. }
  81.  
  82. public void WriteString(string val)
  83. {
  84. if (!this.m_startOfLine)
  85. {
  86. this.m_file.Write(separator);
  87. }
  88. this.m_startOfLine = false;
  89. this.m_file.Write(val);
  90. }
  91.  
  92. public void WriteStringArray(string[] vals)
  93. {
  94. if (!this.m_startOfLine)
  95. {
  96. this.m_file.Write(separator);
  97. }
  98. this.m_startOfLine = false;
  99. bool flag = true;
  100. foreach (string str in vals)
  101. {
  102. if (!flag)
  103. {
  104. this.m_file.Write(subSeparator);
  105. }
  106. flag = false;
  107. this.m_file.Write(str);
  108. }
  109. }
  110. }

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. 洛谷P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1.加号与等号 ...

  2. [BZOJ4338][BJOI2015]糖果(扩展Lucas)

    先求出式子$P_{C_{K+m-1}^{m}}^{n}$,然后对于排列直接$O(n)$求解,对于组合用扩展Lucas求解. 但这题数据并没有保证任何一个模数的质因子的$p^k$在可线性处理的范围内,于 ...

  3. 【插头DP】BZOJ1814-Formula

    [题目大意] 给出一个m*n的矩阵里面有一些格子为障碍物,求经过所有非障碍格子的哈密顿回路个数. [思路] 最典型的插头DP.分为三种情况: (1)当前格子既没有上插头也没有左插头. 如果下边和右边都 ...

  4. 【最小割】BZOJ2039- [2009国家集训队]employ人员雇佣

    [题目大意] 给定n个人,每个人有一个佣金,i和j如果同时被雇佣会产生2*E(i,j)的效益,i和j如果一个被雇佣一个不被雇佣会产生E(i,j)的亏损,求最大收益. [思路] 如果没有亏损,其实非常类 ...

  5. [MVC4]Data Annotations Extensions:无法使用EmailAddress等验证特性的解决方法

    本文地址:http://www.cnblogs.com/egger/p/3404159.html  欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 数据注解(Data Annotations) Web应用 ...

  6. SpringMvc的服务器端跳转和客户端跳转

    首先,找到 package org.springframework.web.servlet.view; public class InternalResourceViewResolver extend ...

  7. 字符串转base64,base64转字符串

    [JavaScript原生提供两个Base64相关方法] btoa():字符串或二进制值转为Base64编码 atob():Base64编码转为原来的编码 备注:利用这两个原生方法,我们来封装一下,标 ...

  8. linux文件系统命令(6)---touch和mkdir

    一.目的 本文将介绍linux下新建文件或文件夹.删除文件或文件夹命令.         touch能够新建文件,mkdir用来新建文件夹.rm用来删除文件或文件夹.         本文将选取ubu ...

  9. mmap函数使用

    UNIX网络编程第二卷进程间通信对mmap函数进行了说明.该函数主要用途有三个:1.将一个普通文件映射到内存中,通常在需要对文件进行频繁读写时使用,这样用内存读写取代I/O读写,以获得较高的性能:2. ...

  10. linux查看端口被哪个服务占用的命令

    netstat -tunpl | grep 6379