Efficient use of C#to separate the CSV column value into different array
This article introduces how to use C#to efficiently separate the column values in the CSV file into different arrays. This is a common task in data processing. We will use StreamReader
class and Split()
functions to achieve this goal.
Reading CSV files and separating its columns into different arrays is a common data operation task. Below is a solution that uses C#to implement this operation.
and
Functions can read CSV files, and each row is divided into different columns according to the segmental partition. Next, we need to create two separate array to store the values of each column.
StreamReader
The following is an example code fragment: Split()
Code Description:
<code class="language-csharp">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]); } } }</code>
The object initializes and opens the CSV file with the specified path.
StreamReader
, and store the values of the first and second columns, respectively. List<string>
listA
For each line, it uses the segment as a separatist segmentation value to generate a value array. listB
while
. listA
will include the values in the csv file, respectively. Then you can use them for further data processing or display on demand. listB
The above is the detailed content of How can I efficiently separate CSV column values into distinct arrays using C#?. For more information, please follow other related articles on the PHP Chinese website!