将几个csv文件的内容保存到可搜索的数组C#中 [英] Save the content of several csv files into a searchable array C#

查看:68
本文介绍了将几个csv文件的内容保存到可搜索的数组C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是

This is a follow up question to Getting specific row and column value in .csv file in c# .

我在一个特定的文件夹中有一堆.csv文件,我希望能够从中读取值:

I have a bunch of .csv-files in a specific folder that I want to be able to read values from:

cars.csv
planes.csv
tanks.csv

对于我创建的 csvfiles 类,我想做的是将每个csv文件的内容以大数组的形式存储在其中,就像我要感谢的文件一样.我上面提到的帮助:

What I want to do is, for my csvfiles class that I have created, to store in it he content of each one of the csv files in a large array like the one I have thanks to the help I got mentioned above:

  string[][] m_Data = File
    .ReadLines(@"c:\MyFile.csv")
    .Where(line => !string.IsNullOrWhiteSpace(line)) 
    .Skip(1)
    .Select(line => line.Split(','))
    .ToArray();

我想以与C ++ std :: map< string,std :: map< int,std :: map< int,double>>等效的方式保存所有文件的内容.>FileMap ,我可以在其中搜索文件名,例如 car ,然后能够从特定位置特定位置的特定文件中获取特定值,例如下面的C ++代码:

I would like is to save the content of all files in some kind of equivalent to a C++ std::map<string, std::map<int, std::map<int, double>>> FileMap where I can search for the file name, like car, and then be able to get a specific value from a specific file at a specific position like in the C++ code below:

double GetVal(string std::filename, int row, int column)
{
   return FileMap[filename][row][column]
}

我该如何在C#中做到这一点?

How do I do that in C#?

edit:.csv文件之一的内容:

edit: Content of one of the .csv-files:

Index,Time,value,,Index,Time,value
1,0,20,,1,0,120
2,1,30,,2,1,90

推荐答案

您可以拥有一个这样的类,该类的_dictionary字段的类型如下例所示,然后从文件中提取数据,存储将它们放在NxM字符串数组中,然后调用PopulateDictionary方法将数据添加到_dictionary字段中.您对每个文件重复此过程.然后,您可以调用method来从_dictionary字段中读取值.

You could have a class like this that has a _dictionary field of the type you can see in the below example, then you extract the data from a file, store them in an NxM array of strings and then you call PopulateDictionary method to add the data to the _dictionary field. You repeat this process for each file. You can then call method to read the values from the _dictionary field.

class Program
{
    static Dictionary<string, Dictionary<int, Dictionary<int, double>>> _dictionary = 
                                new Dictionary<string, Dictionary<int, Dictionary<int, double>>>();


    static void PopulateDictionary(string filename, string[][] data)
    {
        _dictionary.Add(filename, new Dictionary<int, Dictionary<int, double>>());

        for (int i = 0; i < data.Length; i++)
        {
            _dictionary[filename].Add(i, new Dictionary<int, double>());

            for (int j = 0; j < data[i].Length; j++)
            {
                _dictionary[filename][i].Add(j, double.Parse(data[i][j]));
            }
        }
    }

    static double GetVal(string filename, int row, int column)
    {
        return _dictionary[filename][row][column];
    }

    static void Main(string[] args)
    {
        List<string> csvData1 = new List<string>() { "23, 5, 7", "40, 15" };
        List<string> csvData2 = new List<string>() { "40, 80, 112", "13", "20, 65, 9" };

        string[][] dataArr1 = csvData1.Select(x => x.Split(',')).ToArray();
        string[][] dataArr2 = csvData2.Select(x => x.Split(',')).ToArray();
        PopulateDictionary("f1", dataArr1);
        PopulateDictionary("f2", dataArr2);

        Console.WriteLine(GetVal("f1", 0, 0));
        Console.WriteLine(GetVal("f2", 1, 0));
        Console.WriteLine(GetVal("f2", 2, 2));

        Console.ReadKey();
    }
}

我提供了一种Main方法,用于模拟字典的创建,您可以轻松地运行该方法来检查我的解决方案是否满足您的需求.列表代表您应从csv文件读取的行.字符串矩阵表示按行和列组织的数据,这些行和列包含以字符串表示形式的双精度值.然后,我模拟将值添加到提供文件名的字典(应使用要从中提取数据的csv文件的名称)和代表行和列的矩阵.最后,我读取在第一个列表中第一行的第一列中找到的第一个值,然后读取第二个列表中第二行的唯一列中的单个元素,最后读取列表中最后一行的最后一列第二个列表.

I've included a Main method for simulating the creation of a dictionary that you can easily run to check if my solution suits your needs. The lists represent the lines you should read from the csv files. The matrixes of string represent the data organized by rows and columns that contain the double values in string representation. Then, I simulate the adding of the values to the dictionary giving the filename (you should use the name of the csv files from which you extract the data) and the matrix representing the rows and the columns. At the end I read the first value found at the first column of the first row in the first list, then the single element at the unique column of the second row in the second list and at last the last column of the last row in the second list.

这篇关于将几个csv文件的内容保存到可搜索的数组C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆