繁体   English   中英

C# 从 8 列的 csv 文件中读取数据并对其进行排序

[英]C# reading data from a csv file, with 8 columns, and sorting it

所以我试图读取一个 csv 文件,它本质上是一个由单词分隔的数据列表,我到目前为止所做的是使用 ReadAllLines 然后从那里用 text.Split(','); 分隔; 唯一的问题是我只是阅读了这种列表/数组类方法,而不是创建一个实际的数组,所以我不知道如何调用或使用它。 这是我到目前为止所拥有的:

using System;
using System.IO;
public class Earthquake
{
    public double Magnitude { get; set; }
    public string Location { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double depth { get; set; }
    public string date { get; set; }
    public string EventID { get; set; }
    public string URL { get; set; }
    public Earthquake(double magna, string locate, double lat, double longi, double dept, string dat, string Event, string website)
    {
        Magnitude = magna;
        Location = locate;
        Latitude = lat;
        Longitude= longi;
        depth = dept;
        date = dat;
        EventID = Event;
        URL = website;
    }

}
public class ManageData
{

    public int count;
    public void getData()
{
    string[] text = File.ReadAllLines(@"Earthquakes.csv");
    foreach (string word in text[count].Split(','))
    {
        //here i want to put each data in the Earthquake class
    }

}

}

你可以更换

foreach (string word in text[count].Split(','))
    {
        //here i want to put each data in the Earthquake class
    }

使用以下代码行,看看它是否有帮助

foreach (string line in text)
{
  string[] myColumns = line.Split(',');

  EarthQuake eQ = new EarthQuake(myColumns[0],myColumns[1],myColumns[2],myColumns[3],myColumns[4],myColumns[5],myColumns[6],myColumns[7],myColumns[8]);
  //Add eQ into a list you want to save

}

这假设列的顺序相同。 如果顺序与使用此构造函数不同,则使用默认构造函数,然后相应地为 EarthQuake 类的属性赋值。

希望有帮助。

当我需要管理 CSV 文件、 C# CSV Reader 和 Writer时,这个项目对我帮助很大

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM