-2
"0.0.0.0,""0.255.255.255"",""ZZ"""                
"1.0.0.0,""1.0.0.255"",""AU"""
"1.0.1.0,""1.0.3.255"",""CN"""
"1.0.4.0,""1.0.7.255"",""AU"""
"1.0.8.0,""1.0.15.255"",""CN"""
"1.0.16.0,""1.0.31.255"",""JP"""
"1.0.32.0,""1.0.63.255"",""CN"""
"1.0.64.0,""1.0.127.255"",""JP"""
"1.0.128.0,""1.0.255.255"",""TH"""
"1.1.0.0,""1.1.0.255"",""CN"""
"1.1.1.0,""1.1.1.255"",""AU"""
"1.1.2.0,""1.1.63.255"",""CN"""
"1.1.64.0,""1.1.127.255"",""JP"""
"1.1.128.0,""1.1.255.255"",""TH"""

İN EXCEL

0.0.0.0,"0.255.255.255","ZZ"
1.0.0.0,"1.0.0.255","AU"
1.0.1.0,"1.0.3.255","CN"
1.0.4.0,"1.0.7.255","AU"
1.0.8.0,"1.0.15.255","CN"
1.0.16.0,"1.0.31.255","JP"
1.0.32.0,"1.0.63.255","CN"
1.0.64.0,"1.0.127.255","JP"
1.0.128.0,"1.0.255.255","TH"
1.1.0.0,"1.1.0.255","CN"
1.1.1.0,"1.1.1.255","AU"
1.1.2.0,"1.1.63.255","CN"
1.1.64.0,"1.1.127.255","JP"
1.1.128.0,"1.1.255.255","TH"
1.2.0.0,"1.2.2.255","CN"
1.2.3.0,"1.2.3.255","AU"
1.2.4.0,"1.2.127.255","CN"
1.2.128.0,"1.2.255.255","TH"
1.3.0.0,"1.3.255.255","CN"
1.4.0.0,"1.4.0.255","AU"
1.4.1.0,"1.4.127.255","CN"
1.4.128.0,"1.4.255.255","TH"

How can split this CSV file.

For example 0.0.0.0 0.255.255.255 ZZ for first row and how can add datagridview with 3columns

5
  • What have you tried so far? Commented Jun 28, 2018 at 5:48
  • 6
    Possible duplicate of How to properly split a CSV using C# split() function? Commented Jun 28, 2018 at 5:49
  • btw it is csv not cvs Commented Jun 28, 2018 at 5:55
  • By using a dedicated CSV reader, such as CsvHelper, rather than going through the pain of reinventing the wheel. Commented Jun 28, 2018 at 6:01
  • This is not even valid CSV. Commented Jun 28, 2018 at 6:17

3 Answers 3

0

You can do it via the following way..

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(','); // or whatever yur get by reading that file

            listA.Add(values[0]);
            listB.Add(values[1]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You should explain your answer a little bit to make this a good answer
You may chop a record in the middle when a string value has a \r\n in it.
0

A CSV file is either a Tab delimited or a Comma delimited file. That said; you have to read the file line by line and then separate the values available in a line based on the delimiter character. The first line usually appears in a CSV file is usually the headers which you can use in order to produce a KeyValue pair to make your collection more efficient. For example:

 Dictionary<int, Dictionary<String, String>> values = new Dictionary<int, Dictionary<String,String>>();
 using(FileStream fileStream = new FileStream(@"D:\MyCSV.csv", FileMode.Open, FileAccess.Read, FileShare.Read)) {
     using(StreamReader streamReader = new StreamReader(fileStream)){
          //You can skip this line if there is no header
          // Then instead of Dictionary<String,String> you use List<String>
          var headers = streamReader.ReadLine().Split(',');
          String line = null;
          int lineNumber = 1;
          while(!streamReader.EndOfStream){
               line = streamReader.ReadLine().split(',');
               if(line.Length == headers.Length){
                   var temp = new Dictionary<String, String>();
                   for(int i = 0; i < headers.Length; i++){
                      // You can remove '"' character by line[i].Replace("\"", "") or through using the Substring method
                      temp.Add(headers[i], line[i]);
                   }
                   values.Add(lineNumber, temp);
               }
               lineNumber++;
          }              
     }

In case the data structure of your CSV is constant and it will not change in the future, you can develop a strongly typed data model and get rid of the Dictionary type. This approach will be more elegant and more efficient.

9 Comments

CSV by definition is comma separated values. The similar structure text files that use TAB as delimiter are not CSV.
@PepitoSh: In data mining projects, sometimes a comma delimited CSV is troublesome, so we go for a Tab Delimited one. That is why I mentioned it.
You should not split the line you read by commas. You may split a string value that has comma in it. Also, you may read a partial record as \r\n may be part of a string value. Parsing CSV is a bit more complicated.
@PepitoSh: You may split a string value that has comma in it, Why? it is the line that is delimited by ',' not the individual records!
No. In CSV columns are delimited with comma. See Specification: en.wikipedia.org/wiki/Comma-separated_values And I concur, tabs may be used as delimiters. :)
|
0

First of all, your CSV lines are surrounded by quotes. Is it copy/paste mistake? If not, you will need to sanitize the file to a valid CSV file.

You can try Cinchoo ETL - an open source library to load the CSV file to datatable, then you can assign it to your DataGridView source.

I'll show you both approach, how to handle

Valid CSV: (test.csv)

0.0.0.0,"0.255.255.255","ZZ"
1.0.0.0,"1.0.0.255","AU"
1.0.1.0,"1.0.3.255","CN"
1.0.4.0,"1.0.7.255","AU"
1.0.8.0,"1.0.15.255","CN"
1.0.16.0,"1.0.31.255","JP"
1.0.32.0,"1.0.63.255","CN"
1.0.64.0,"1.0.127.255","JP"
1.0.128.0,"1.0.255.255","TH"
1.1.0.0,"1.1.0.255","CN"
1.1.1.0,"1.1.1.255","AU"
1.1.2.0,"1.1.63.255","CN"
1.1.64.0,"1.1.127.255","JP"
1.1.128.0,"1.1.255.255","TH"

Read CSV:

using (var p = new ChoCSVReader("test.csv"))
{
    var dt = p.AsDataTable();
    //Assign dt to DataGridView
}

Next approach

Invalid CSV: (test.csv)

"0.0.0.0,""0.255.255.255"",""ZZ"""
"1.0.0.0,""1.0.0.255"",""AU"""
"1.0.1.0,""1.0.3.255"",""CN"""
"1.0.4.0,""1.0.7.255"",""AU"""
"1.0.8.0,""1.0.15.255"",""CN"""
"1.0.16.0,""1.0.31.255"",""JP"""
"1.0.32.0,""1.0.63.255"",""CN"""
"1.0.64.0,""1.0.127.255"",""JP"""
"1.0.128.0,""1.0.255.255"",""TH"""
"1.1.0.0,""1.1.0.255"",""CN"""
"1.1.1.0,""1.1.1.255"",""AU"""
"1.1.2.0,""1.1.63.255"",""CN"""
"1.1.64.0,""1.1.127.255"",""JP"""
"1.1.128.0,""1.1.255.255"",""TH"""

Read CSV:

using (var p = new ChoCSVReader("Sample6.csv"))
{
    p.SanitizeLine += (o, e) =>
    {
        string line = e.Line as string;
        if (line != null)
        {
            line = line.Substring(1, line.Length - 2);
            line = line.Replace(@"""""", @"""");
        }

        e.Line - line;
    };

    var dt = p.AsDataTable();
    //Assign dt to DataGridView
}

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.