C# 如何分割CSV文件

C# 如何分割CSV文件,c#,csv,split,C#,Csv,Split,İ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"""

İ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"""
如何拆分此CSV文件


例如,第一行为0.0.0.0 0.255.255.255 ZZ,以及如何添加带有3列的datagridview。您可以通过以下方式执行此操作

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"
CSV文件可以是制表符分隔的文件,也可以是逗号分隔的文件。也就是说;您必须逐行读取文件,然后根据分隔符分隔行中的可用值。CSV文件中通常出现的第一行通常是标题,您可以使用这些标题生成键值对,以提高收集效率。例如:

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]);
        }
    }
}

如果CSV的数据结构是恒定的,并且将来不会更改,则可以开发强类型数据模型并去掉字典类型。这种方法将更加优雅和高效

首先,您的CSV行被引号包围。是复制/粘贴错误吗?如果没有,则需要将文件清理为有效的CSV文件

您可以尝试使用开源库将CSV文件加载到datatable,然后将其分配给DataGridView源

我会告诉你们两个方法,如何处理

有效CSV:test.CSV

阅读CSV:

下一个方法

无效的CSV:test.CSV

阅读CSV:


希望能有所帮助。

到目前为止您尝试了什么?顺便说一句,它可能是csv的副本,而不是使用专用的csv阅读器,例如,这不是一个有效的CSV。你应该解释一下你的答案,让它成为一个好答案。当字符串值中有一个r\n时,你可以在中间砍掉一个记录。使用制表符作为分隔符的类似结构文本文件不是CSV。@PepitoSh:在数据挖掘项目中,有时逗号分隔的CSV很麻烦,所以我们选择制表符分隔的CSV。这就是我提到它的原因。你不应该用逗号分隔你读到的行。可以拆分包含逗号的字符串值。此外,您还可以读取部分记录,\r\n因为它可能是字符串值的一部分。解析CSV有点复杂。@PepitoSh:您可以拆分一个包含逗号的字符串值,为什么?它是由“,”分隔的行,而不是单个记录!否。CSV中的列用逗号分隔。请参阅规范:我同意,制表符可以用作分隔符:
 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++;
          }              
     }
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"
using (var p = new ChoCSVReader("test.csv"))
{
    var dt = p.AsDataTable();
    //Assign dt to DataGridView
}
"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"""
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
}