How to select elements within a csv in C #?

1

My problem is this, I have a csv with two lines separated at the end by; and each word separated by, such that:

prueba.csv

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

Now I want to select the first row and put in variables each name for example string cat = cat that comes from the csv only from the first row and then the same with the second I tried with this code but only the letters are catching me:

c #

   static void Main(string[] args)
    {

        int contador;
        string datos;


        var ruta = Directory.GetCurrentDirectory();

        ruta = ruta + "/prueba.csv";


        var lineas = File.ReadAllText(ruta);

        Console.Write(lineas.Split(';'));


            Console.ReadKey();



    }
    
asked by ortiga 17.07.2018 в 15:54
source

1 answer

1

Having these test values:

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

I have modified the code to achieve what I understand you want to achieve with your program:

Example:

static void Main(string[] args)
{
   int contador;
   string datos;

   // Arreglo de strings que guardará las líneas que terminen en punto y coma (;).
   string[] arreglo_lineas = null;

   var ruta = Directory.GetCurrentDirectory();
   ruta = ruta + "/prueba.csv";
   arreglo_lineas = File.ReadAllText(ruta).Split(';');

   Console.log('Esta es la primera línea: ' + arreglo_lineas[0]);
   // Imprimiría: Esta es la primera línea: gato,casa,http,antonio;

   Console.ReadKey();

}
    
answered by 17.07.2018 / 16:51
source