简体   繁体   中英

How to retrieve only few columns data of a csv using the column names instead of column number in c#

I have a csv consisting of many columns. From that csv I have to select only few required columns.

The code I have written is

                for (int i = 0; i < lineCount; i++)
                {
                    var line = str.ReadLine();
                    if (line != null)
                    {
                        var values = line.Split(',');
                        dataInformation.Add(new DataInformation
                        {
                            timestamp_iso = values[3],
                            last_attributed_touch_data_tilde_campaign = values[9],
                            last_attributed_touch_data_tilde_channel = values[11],
                            last_attributed_touch_data_tilde_feature = values[12],
                            last_attributed_touch_data_tilde_ad_set_name = values[19],
                            user_data_platform = values[69],
                            user_data_aaid = values[70],
                            user_data_idfa = values[71],
                            user_data_idfv = values[72]
                        });
                    }
                } 

I am getting wrong values while using this. Is there any other approach to retrieve the values using the column names instead of column numbers.

The Data Information is a class

public class DataInformation
    {
        public string timestamp_iso { get; set; }
        public string last_attributed_touch_data_tilde_campaign { get; set; }
        public string last_attributed_touch_data_tilde_channel { get; set; }
        public string last_attributed_touch_data_tilde_feature { get; set; }
        public string last_attributed_touch_data_tilde_ad_set_name { get; set; }
        public string user_data_platform { get; set; }
        public string user_data_aaid { get; set; }
        public string user_data_idfa { get; set; }
        public string user_data_idfv { get; set; }
    }

Please help me on this.

I recommend using a library to deal with CSV format. CsvHelper is a good one. It allows accessing fields by column name:

csv.Read();
var field = csv["HeaderName"];

CSV format may look simple, but there are a few corner cases (like quotes), so it is better to use an existing solution.

I have used the below code to get all the records of the type DataInformation.

            using (TextReader fileReader = File.OpenText(FileName))
            {
                var csv = new CsvReader(fileReader);
                dataInformation = csv.GetRecords<DataInformation>().ToList();
            }

And after that I have used the below code to get the required columns.

            using (TextWriter writer = new StreamWriter(ConfigurationManager.AppSettings["downloadFilePath"] + ConfigurationManager.AppSettings["fileName"] + date + ConfigurationManager.AppSettings["csvExtension"].ToString()))
            {
                using (var csv = new CsvWriter(TextWriter.Synchronized(writer)))
                {
                    csv.WriteHeader(typeof(DataInformation));
                    csv.NextRecord();
                    csv.WriteRecords(dataInformation);
                }
            }

It works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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