Home > other >  Reading dynamic header from csv file
Reading dynamic header from csv file

Time:07-02

I am working on an application where I send requests to get API which returns CSV with a large set of data. I need to read the list of data from CSV but the data headers are not consistent though they are the same their row number can be changed. I am trying to get the list of data from YEAR, MO, DY, HR, ALLSKY_SFC_SW_DWN, CLRSKY_SFC_SW_DWN. The column headers will be the same always. I want to dynamically map the list of records to my DTO for that I have implemented the code but always gives me an error for example YEAR[0] doesn't exist.

public class ResponseDTO
    {
        public int Year { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
        public int Hour { get; set; }
        public decimal ALLSKY_SFC_SW_DWN { get; set; }
        public decimal CLRSKY_SFC_SW_DWN { get; set; }

    }

    public sealed class ResponseDTOMap : ClassMap<ResponseDTO>
    {
        public ResponseDTOMap()
        {
            Map(m => m.Year).Name(("YEAR"));
            Map(m => m.Month).Name("MO");
            Map(m => m.Day).Name("DY");
            Map(m => m.Hour).Name("HR");
            Map(m => m.ALLSKY_SFC_SW_DWN).Name("ALLSKY_SFC_SW_DWN");
            Map(m => m.CLRSKY_SFC_SW_DWN).Name("CLRSKY_SFC_SW_DWN");
        }
    }




 public async Task<IEnumerable<ResponseDTO>> GetDataAsync<T>(string url)
        {
            DefaultRequestHeaders.Clear();
            DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue() { NoCache = true };
            using (var reader = new StreamReader(await GetStreamAsync(url)))
            {
                using (var csvReader = new CsvReader(reader, CultureInfo.CurrentCulture))
                {
                    csvReader.Context.RegisterClassMap<ResponseDTOMap>();
                    var records = csvReader.GetRecords<ResponseDTO>().ToList();
                    return records;

                }
            }

        }

Here is the image of the CSV file. I need to read the data below the highlighted area. enter image description here

CodePudding user response:

You could try as below to make the pointer on the right place

 using (var reader = new StreamReader(file.OpenReadStream()))
                {
                    reader.ReadLine();
                    // it was not used to read text but adjust  the position of pointer, in your case,you need add the codes for 10 times 
                    reader.ReadLine();
                    reader.ReadLine();
                    using (var csvReader = new CsvReader(reader, CultureInfo.CurrentCulture))
                    {
                        var records = csvReader.GetRecords<TestModel>().ToList();
                    }
    
                    
                } 

Result: enter image description here

also,open your csv file as below and check if the name contains sapce,it may also cause the error you've shown Correct: enter image description here Wrong: enter image description here

  • Related