我正在尝试读取此 csv 文件,但我一直在获取超出索引的范围,关于如何避免它的任何想法?

Am trying to read this csv file but I keep getting a range out of index, any ideas on how to avoid it?

我正在尝试读取此 csv 文件,稍后我将在其中选择不同的元素并创建一个事实 table 如果每个元素都存在于一行中,我将 1 如果不是 0 line/row 长度保持不变,这就是

遇到的问题
chicken,other vegetables,packaged fruit/vegetables,condensed milk,frozen vegetables,fruit/vegetable juice
vinegar,oil
rolls/buns,soda,specialty bar
whole milk
pork,root vegetables,whole milk,whipped/sour cream
rolls/buns,newspapers
grapes,other vegetables,zwieback,Instant food products,dishes
frankfurter,citrus fruit,whipped/sour cream,cream cheese ,rolls/buns,baking powder,seasonal products,napkins
finished products,root vegetables,packaged fruit/vegetables,yogurt,specialty cheese,frozen vegetables,ice cream,soda,bottled beer
onions,other vegetables,flour
tropical fruit,whole milk,rolls/buns

我正在使用以下代码

static void Main(string[] args)
{
    string filepath = @"C:\Downloads\groceriess.csv";
    DataTable res = ConvertCSVtoDataTable(filepath);

    DataTable ConvertCSVtoDataTable(string strFilePath)
    {
        StreamReader sr = new StreamReader(strFilePath);
        string[] headers = sr.ReadLine().Split(',');
        DataTable dt = new DataTable();
        foreach (string header in headers)
        {
            dt.Columns.Add(header);
        }
        while (!sr.EndOfStream)
        {
            string[] rows = Regex.Split(sr.ReadLine(), ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
            DataRow dr = dt.NewRow();
            for (int i = 0; i < headers.Length; i++)
            { //i did try i<rows.Length and also i < headers.Length && i <rows.Length
              dr[i] = rows[i]; //this is the line that is causing the error
            }
            dt.Rows.Add(dr);
        }
        return dt;
     }
}

您的代码意味着一行中的项目数必须正好等于header.length。但是在您的 CSV 文件中,每一行的长度与其他行不同。

您必须根据 header 长度分别检查每行中的项目数。

看起来问题中的代码旨在读取具有 header 行和固定列数的 CSV 文件。但是,那不是您的数据。

最好的办法是将数据存储在 List<List<string>> 中,如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Groceries
{
    internal class Program
    {
        static List<List<string>> LoadGroceryList(string filename)
        {
            var groceryList = new List<List<string>>();

            using (var reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var items = Regex.Split(line, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
                    if (items.Length > 0) { groceryList.Add(items.ToList()); }
                }
            }

            return groceryList;

        }

        static void Main(string[] args)
        {
            string filepath = @"C:\temp\Groceries.csv";
            var groceries = LoadGroceryList(filepath);

            var uniqueItems = groceries.SelectMany(x => x).Distinct().OrderBy(y => y);

            Console.WriteLine(string.Join("\r\n", uniqueItems));

            Console.ReadLine();

        }
    }
}

对于问题中的数据,输出:

baking powder
bottled beer
chicken
citrus fruit
condensed milk
cream cheese
dishes
finished products
flour
frankfurter
frozen vegetables
fruit/vegetable juice
grapes
ice cream
Instant food products
napkins
newspapers
oil
onions
other vegetables
packaged fruit/vegetables
pork
rolls/buns
root vegetables
seasonal products
soda
specialty bar
specialty cheese
tropical fruit
vinegar
whipped/sour cream
whole milk
yogurt
zwieback