Unity3D使用C#解析csv文件

2017-01-02· 5862 次浏览
Unity3D解析csv文件,可以指定字符集,基于IO流读取,解决了GBK乱码问题,支持双引号,支持json字符串。 代码 ``` using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; /// <summary> /// 解析CSV文件 /// CSV.Parse("/hello.csv") 代表Assets下的hello.csv文件 /// </summary> public class CSV {       public static List<List<string>> Parse(string path) {         return Parse(path, "GBK");     }     public static List<List<string>> Parse(string path,string encoding){           StreamReader sr = new StreamReader(Application.dataPath + path, UnicodeEncoding.GetEncoding(encoding));         string text = sr.ReadToEnd();         sr.Close();           if (text == null)             return null;           List<List<string>> result = new List<List<string>>();         List<string> line = new List<string>();         string field = "";         bool isInQuotation = false;//字符串模式         bool isInField = true;//是否在读取Field,用来表示空Field         int i = 0;         while (i < text.Length){             char ch = text[i];             if (isInQuotation){                 if (ch == '"')                 {                     if (i < text.Length - 1 && text[i + 1] == '"')//重复"只算一个,切不结束字符串模式                     {                         field += '"';                         i++;                     }                     else                     {                         isInQuotation = false;                     }                 }                 else                 {                     field += ch;//字符串模式中所有字符都要加入                 }             }             else             {                 switch (ch)                 {                     case ',':                         line.Add(field);                         field = "";                         isInField = true;                         break;                     case '"':                         if (isInField)                             isInQuotation = true;//进入字符串模式                         else                             field += ch;                         break;                     case '\r':                         if (field.Length > 0 &##124;&##124; isInField)                         {                             line.Add(field);                             field = "";                         }                         result.Add(line);                         line = new List<string>();                         isInField = true;//下一行首先应该是数据                         if (i < text.Length - 1 && text[i + 1] == '\n')//跳过\r\n                             i++;                         break;                     default:                         isInField = false;                         field += ch;                         break;                 }             }             i++;         }         //收尾工作         if (field.Length > 0 &##124;&##124; isInField && line.Count > 0)//如果是isInField标记的单元格,则要保证这行有其他数据,否则单独一个空单元格的行是没有意义的             line.Add(field);           if (line.Count > 0)             result.Add(line);           return result;     }   } ```