Home > Backend Development > C++ > How can I efficiently separate CSV column values into distinct arrays using C#?

How can I efficiently separate CSV column values into distinct arrays using C#?

Susan Sarandon
Release: 2025-01-26 07:01:08
Original
480 people have browsed it

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.

How can I efficiently separate CSV column values into distinct arrays using C#?

Storage of the value of the CSV file into the separate array

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>
Copy after login

The object initializes and opens the CSV file with the specified path.

    Create two collection,
  • and StreamReader, and store the values ​​of the first and second columns, respectively.
  • Each line of the circular iteration file until the end of the file is read. List<string> listA For each line, it uses the segment as a separatist segmentation value to generate a value array. listB
  • The first value is added to
  • , and the second value is added to while.
  • After the cycle is completed,
  • and listA will include the values ​​in the csv file, respectively. Then you can use them for further data processing or display on demand. listB
  • This method is simple and efficient, suitable for most CSV file processing scenarios. Of course, for more complicated cases, such as CSV files containing quotes or other special characters, more robust analysis methods may be needed.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template