【问题标题】:How to split CSV file如何拆分 CSV 文件
【发布时间】:2018-06-28 05:39:45
【问题描述】:
"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"

如何拆分这个 CSV 文件。

例如0.0.0.0 0.255.255.255 ZZ 第一行以及如何添加具有 3 列的 datagridview

【问题讨论】:

标签: c# csv split


【解决方案1】:

你可以通过以下方式做到这一点..

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

【讨论】:

  • 您应该稍微解释一下您的答案以使其成为一个好的答案
  • 当字符串值中包含 \r\n 时,您可以在中间截断记录。
【解决方案2】:

CSV 文件是 Tab delimited 或 Comma delimited 文件。那就是说;您必须逐行读取文件,然后根据delimiter 字符分隔一行中可用的值。第一行通常出现在 CSV 文件中,通常是您可以用来生成 KeyValue 对的标题,从而使您的收藏更有效率。例如:

 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++;
          }              
     }

如果你的CSV的数据结构是不变的,以后不会改变,你可以开发一个强类型的数据模型,去掉Dictionary这个类型。这种方法将更加优雅和高效。

【讨论】:

  • CSV 根据定义是逗号分隔值。使用 TAB 作为分隔符的类似结构文本文件是 not CSV。
  • @PepitoSh:在数据挖掘项目中,有时逗号分隔的 CSV 很麻烦,所以我们选择制表符分隔的。这就是我提到它的原因。
  • 你不应该用逗号分割你阅读的行。您可以拆分包含逗号的字符串值。此外,您可能会读取部分记录,因为 \r\n 可能是字符串值的一部分。解析 CSV 有点复杂。
  • @PepitoSh: You may split a string value that has comma in it,为什么?它是由 '' 分隔的行,而不是单个记录!
  • 没有。在 CSV 中,列用逗号分隔。请参阅规范:en.wikipedia.org/wiki/Comma-separated_values 我同意,制表符可以用作分隔符。 :)
【解决方案3】:

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

您可以尝试Cinchoo ETL - 一个将 CSV 文件加载到数据表的开源库,然后您可以将其分配给您的 DataGridView 源。

我会告诉你这两种方法,如何处理

有效 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"

读取 CSV:

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

下一个方法

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"""

读取 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
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多