第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用結(jié)構(gòu)體和列表來存儲(chǔ) csv 文件的部分內(nèi)容

使用結(jié)構(gòu)體和列表來存儲(chǔ) csv 文件的部分內(nèi)容

PHP
ABOUTYOU 2024-01-20 21:49:01
因此,我必須在本作業(yè)中使用一個(gè)結(jié)構(gòu)體,并且需要對(duì)一個(gè)名為 Country_databas.csv 的 CSV 文件進(jìn)行排序。到目前為止,我已經(jīng)制作了該結(jié)構(gòu)體和一個(gè)列表,用于處理 CSV 上一行的每個(gè)分割。但是,我正在努力將 CSV 的每個(gè)拆分添加到其各自的列表中,以便我可以對(duì)它們進(jìn)行排序。編輯:抱歉,我剛剛意識(shí)到我錯(cuò)過了 CSV 在線的最后一個(gè)分割,即第一行的“Kabul”。我的 CSV 的前 4 行:1. Afghanistan,Asia,652230,25500100,20364000000,Kabul2. Albania,Europe,28748,2821977,12044000000,Tirana3. Algeria,Africa,2381741,38700000,207021000000,Algiers4. 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);        }      }    }  }}這會(huì)導(dǎo)致錯(cuò)誤“無法讀取文件 System.NullReferenceException:對(duì)象引用未設(shè)置到對(duì)象的實(shí)例”,該錯(cuò)誤來自行“csv.Country.Add(values[1]);”
查看完整描述

1 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的結(jié)構(gòu)錯(cuò)誤,您不應(yīng)在結(jié)構(gòu)中添加列表,結(jié)構(gòu)的每條記錄應(yīng)僅包含一個(gè)國(guó)家/地區(qū)的條目,然后您可以像這樣創(chuàng)建結(jié)構(gòu)列表:


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)

{

    this.Country = Country;

    this.Continent = Continent;

    this.popualtion = popualtion;

    this.landmass = landmass;

    this.Bignum = Bignum;

}



}

那么你可以這樣做:


    string line;

    var csv  = new List<CSVline>();

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

    {

        try

        { 

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

            {

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

                csv.Add(new CSVline(values[0],values[1],values[2],values[3],values[4]));




            }

        }

        catch (Exception ex)

        {

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

        }

      }


查看完整回答
反對(duì) 回復(fù) 2024-01-20
  • 1 回答
  • 0 關(guān)注
  • 215 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)