1

I have a CSV with the following data (no header)

12,2010,76
2,2000,45
12,1940,30

and I'm using the following CSVReader to read

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;
    }
}

The problem is this CSVReader uses List<Dictionary<string, object>> and so if there is no header information, the dictionary keys become either null or (less likely) an empty string. Both of these cases would lead to exception throwing when adding entries to the dictionary.

I can add the headers to the CSV file but that isn't the ideal solution.

3
  • 1
    Can you change the library to CsvHelper, its extemely fast and easy. Commented Nov 7, 2019 at 12:03
  • Even though I'm using Unity, I think i can. Just didn't know about it and how to read data using it. Commented Nov 7, 2019 at 12:08
  • This has everything you need joshclose.github.io/CsvHelper/getting-started Commented Nov 7, 2019 at 12:17

1 Answer 1

3

Firstly, there are almost certainly lots of ready-made libraries you could use for this task, which are probably available on NuGet. One of those might well be a better solution.

Nevertheless, working with what you've got already you could make an alternative version of the method which returns a simple list of objects, and remove the code from it which populates the headers. Something like this (untested, but I think it should work):

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

In ReadWithoutHeader(string file) I get an error saying not all code paths return a value
Sorry. If you compare it to the original you'll see that I just forgot to add return list; at the end. Did you spot that? I will amend the answer
You spotted faster than me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.