How to save in variable fields of a csv in c # and delete the first line?

2

In my code I pass a number of lines that I want you to take from csv and then,  for each line I iterate the elements that make up the csv but I'm not getting it, I put the code

prueba.csv

gato,casa,http,antonio;
perro,abanico,https,libro;

C #    class Program     {

    static void Main(string[] args)
    {


        int contador = 0;
        int num_lineas;
        string[] array = null;


        var ruta = Directory.GetCurrentDirectory();

        ruta += "/articulo.csv";



        num_lineas = File.ReadAllLines(ruta).Length;



        array = File.ReadAllText(ruta).Split(';');

        while (contador < num_lineas)
        {


            while (true)
            {

                var a = array[contador].Split(',')[0];
                var b = array[contador].Split(',')[1];
                var c = array[contador].Split(',')[2];
                var d = array[contador].Split(',')[3];
                var e = array[contador].Split(',')[4];
                var f = array[contador].Split(',')[5];
                var g = array[contador].Split(',')[6];
                var h = array[contador].Split(',')[7];
                var i = array[contador].Split(',')[8];

                var imagen = array[contador].Split(',')[9];



                Console.WriteLine(c);



                Thread.Sleep(6000);
                break;

            }

            File.Replace(array[contador]); //aqui tengo que borrar la linea que esta dentro del archivo csv


            contador++;

        }





        Thread.Sleep(6000);


    }
}

}

I have modified it so I want to now delete the first line of the csv any idea ....

    
asked by ortiga 17.07.2018 в 17:53
source

1 answer

2

Another solution could be to use Linq to read all the lines and in turn enter them into a list of a defined class (for better manipulation of objects) instead of a array . For example, having the following class:

public class Lista
{
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
    public string d { get; set; }
    public string e { get; set; }
    public string f { get; set; }
    public string g { get; set; }
    public string h { get; set; }
    public string i { get; set; }
    public string imagen { get; set; }
}

When using Linq, you can read the file, omit the first line and in turn, evaluate that the content of the file contains the positions and an appropriate length.

var ruta = Directory.GetCurrentDirectory() + "/articulo.csv";

var lista = (from p in System.IO.File.ReadAllLines("").Skip(1)
                  // El método .Skip(1) omite la primer línea que sería el encabezado
              let parts = p.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
              where parts.Length == 10
              select new Lista
              {
                  a = parts[0].ToString(),
                  b = parts[1].ToString(),
                  c = parts[2].ToString(),
                  d = parts[3].ToString(),
                  e = parts[4].ToString(),
                  f = parts[5].ToString(),
                  g = parts[6].ToString(),
                  h = parts[7].ToString(),
                  i = parts[8].ToString(),
                  imagen = parts[9].ToString(),
              });       
    
answered by 17.07.2018 / 23:41
source