使用结构体和列表来存储 csv 文件的部分内容

因此,我必须在本作业中使用一个结构体,并且需要对一个名为 Country_databas.csv 的 CSV 文件进行排序。到目前为止,我已经制作了该结构体和一个列表,用于处理 CSV 上一行的每个分割。但是,我正在努力将 CSV 的每个拆分添加到其各自的列表中,以便我可以对它们进行排序。


编辑:抱歉,我刚刚意识到我错过了 CSV 在线的最后一个分割,即第一行的“Kabul”。


我的 CSV 的前 4 行:


1. Afghanistan,Asia,652230,25500100,20364000000,Kabul

2. Albania,Europe,28748,2821977,12044000000,Tirana

3. Algeria,Africa,2381741,38700000,207021000000,Algiers

4. Andorra,Europe,468,76098,3222000000,Andorra la Vella

代码:


namespace CSVFileUtility

{


  public struct CSVline

  {

    public List<string> Country;

    public List<string> Continent;

    public List<int> popualtion;

    public List<int> landmass;

    public List<long> Bignum;


    public CSVline(List<string> Country, List<string> Continent, List<int> popualtion, List<int> landmass, List<long> Bignum)

    {

      this.Country = Country;

      this.Continent = Continent;

      this.popualtion = popualtion;

      this.landmass = landmass;

      this.Bignum = Bignum;

    }

  }


  class Program

  {

    static void Main(string[] args)

    {

      string line;

      CSVline csv = new CSVline();


      using ( StreamReader sr = new StreamReader(@"filepath of csv") )

      {

        try

        {

          while ( ( line = sr.ReadLine() ) != null )

          {

            var values = line.Split(',');

            csv.Country.Add(values[1]);

          }

        }

        catch ( Exception ex )

        {

          Console.WriteLine("The file could not be read {0}", ex);

        }


      }

    }

  }


}

这会导致错误“无法读取文件 System.NullReferenceException:对象引用未设置到对象的实例”,该错误来自行“csv.Country.Add(values[1]);”


ABOUTYOU
浏览 51回答 1
1回答

慕侠2389804

您的结构错误,您不应在结构中添加列表,结构的每条记录应仅包含一个国家/地区的条目,然后您可以像这样创建结构列表:public struct CSVline{public string Country;public string Continent;public int popualtion;public int landmass;public long Bignum;public CSVline(string Country, string Continent, int popualtion, int landmass, long Bignum){&nbsp; &nbsp; this.Country = Country;&nbsp; &nbsp; this.Continent = Continent;&nbsp; &nbsp; this.popualtion = popualtion;&nbsp; &nbsp; this.landmass = landmass;&nbsp; &nbsp; this.Bignum = Bignum;}}那么你可以这样做:&nbsp; &nbsp; string line;&nbsp; &nbsp; var csv&nbsp; = new List<CSVline>();&nbsp; &nbsp; using (StreamReader sr = new StreamReader(@"filepath of csv"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = sr.ReadLine()) != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var values = line.Split(',');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csv.Add(new CSVline(values[0],values[1],values[2],values[3],values[4]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("The file could not be read {0}", ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP