Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading CSV file and storing values into an array

Tags:

arrays

c#

.net

csv

I am trying to read a *.csv-file.

The *.csv-file consist of two columns separated by semicolon (";").

I am able to read the *.csv-file using StreamReader and able to separate each line by using the Split() function. I want to store each column into a separate array and then display it.

Is it possible to do that?

like image 737
Rushabh Shah Avatar asked Mar 12 '11 14:03

Rushabh Shah


People also ask

How will you read CSV data into an array in NumPy?

To read CSV data into a record in a Numpy array you can use the Numpy library genfromtxt() function, In this function's parameter, you need to set the delimiter to a comma. The genfromtxt() function is used quite frequently to load data from text files in Python.

How do I convert a CSV file to a matrix in Python?

Line 1: We import the NumPy library. Line 3-4: We open the sampleCSV file and we pass both CSVData and the delimiter to NumPy np. genfromtxt () function, which returns the data into a 2D array. Line 6: We finally print the result which shows that now our CSV data converted into a 2D array.


1 Answers

You can do it like this:

using System.IO;  static void Main(string[] args) {     using(var reader = new StreamReader(@"C:\test.csv"))     {         List<string> listA = new List<string>();         List<string> listB = new List<string>();         while (!reader.EndOfStream)         {             var line = reader.ReadLine();             var values = line.Split(';');              listA.Add(values[0]);             listB.Add(values[1]);         }     } } 
like image 184
Michael M. Avatar answered Sep 30 '22 01:09

Michael M.