C# 如何读取没有标题的CSV

C# 如何读取没有标题的CSV,c#,csv,C#,Csv,我有一个包含以下数据的CSV(无标题) 我用下面的内容来阅读 使用UnityEngine; 使用制度; 使用系统集合; 使用System.Collections.Generic; 使用System.Text.RegularExpressions; 公共类CSVReader { 静态字符串拆分_RE=@“,(?=(?:[^”“]*”“[^”“]*”)*(?![^”“]*)”; 静态字符串行_SPLIT_RE=@“\r\n | \n\r | \n | \r”; 静态字符[]修剪字符={'\''};

我有一个包含以下数据的CSV(无标题)

我用下面的内容来阅读

使用UnityEngine;
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
公共类CSVReader
{
静态字符串拆分_RE=@“,(?=(?:[^”“]*”“[^”“]*”)*(?![^”“]*)”;
静态字符串行_SPLIT_RE=@“\r\n | \n\r | \n | \r”;
静态字符[]修剪字符={'\''};
公共静态列表读取(字符串文件)
{
var list=新列表();
TextAsset data=Resources.Load(文件)作为TextAsset;
var LINE=Regex.Split(data.text,LINE\u Split\RE);

如果(lines.Length首先,几乎可以肯定有很多现成的库可用于此任务,它们可能在NuGet上提供。其中之一可能是更好的解决方案

尽管如此,使用您已有的方法,您可以创建该方法的另一个版本,该版本返回一个简单的对象列表,并从中删除填充标题的代码

publicstaticlist ReadWithoutHeader(字符串文件)
{
var list=新列表();
TextAsset data=Resources.Load(文件)作为TextAsset;
var LINE=Regex.Split(data.text,LINE\u Split\RE);

如果(lines.Length)您可以将库更改为
CsvHelper
,它非常快速和简单。即使我使用Unity,我想我可以。只是不知道它以及如何使用它读取数据。这在
ReadWithoutHeader(字符串文件)中有您需要的一切
我收到一个错误,说
不是所有的代码路径都返回值
对不起。如果你将其与原始路径进行比较,你会发现我只是忘记在末尾添加
返回列表;
。你发现了吗?我会比我更快地修改你发现的答案
12,2010,76
2,2000,45
12,1940,30
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class CSVReader
{
    static string SPLIT_RE = @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))";
    static string LINE_SPLIT_RE = @"\r\n|\n\r|\n|\r";
    static char[] TRIM_CHARS = { '\"' };

    public static List<Dictionary<string, object>> Read(string file)
    {
        var list = new List<Dictionary<string, object>>();
        TextAsset data = Resources.Load (file) as TextAsset;

        var lines = Regex.Split (data.text, LINE_SPLIT_RE);

        if(lines.Length <= 1) return list;

        var header = Regex.Split(lines[0], SPLIT_RE);
        for(var i=1; i < lines.Length; i++) {

            var values = Regex.Split(lines[i], SPLIT_RE);
            if(values.Length == 0 ||values[0] == "") continue;

            var entry = new Dictionary<string, object>();
            for(var j=0; j < header.Length && j < values.Length; j++ ) {
                string value = values[j];
                value = value.TrimStart(TRIM_CHARS).TrimEnd(TRIM_CHARS).Replace("\\", "");
                object finalvalue = value;
                int n;
                float f;
                if(int.TryParse(value, out n)) {
                    finalvalue = n;
                } else if (float.TryParse(value, out f)) {
                    finalvalue = f;
                }
                entry[header[j]] = finalvalue;
            }
            list.Add (entry);
        }
        return list;
    }
}
public static List<List<object>> ReadWithoutHeader(string file)
{
    var list = new List<List<object>>();
    TextAsset data = Resources.Load (file) as TextAsset;
    var lines = Regex.Split (data.text, LINE_SPLIT_RE);

    if(lines.Length <= 1) return list;

    for(var i=0; i < lines.Length; i++) {

        var values = Regex.Split(lines[i], SPLIT_RE);
        if(values.Length == 0 ||values[0] == "") continue;
        var entry = new List<object>();

        for(var j=0; j < values.Length; j++ ) {
            string value = values[j];
            value = value.TrimStart(TRIM_CHARS).TrimEnd(TRIM_CHARS).Replace("\\", "");
            object finalvalue = value;
            int n;
            float f;
            if(int.TryParse(value, out n)) {
                finalvalue = n;
            } else if (float.TryParse(value, out f)) {
                finalvalue = f;
            }
            entry.Add(finalvalue);
        }
        list.Add(entry);
    }
    return list;
}