使用标题拆分CSV文件。 [英] Splitting CSV file with headers.

查看:98
本文介绍了使用标题拆分CSV文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试按数据中的来源拆分CSV,但保留标题。我的原始代码是 -





Hello, I am trying to split a CSV by a source in the data but keep the headers. The orginal code I had was -


var splitQuery = from line in File.ReadLines(@"C:\test\test1.csv")
            let source = line.Split(',').Last()
            group line by source into outputs
            select outputs;

foreach (var output in splitQuery)
{
    File.WriteAllLines(@"C:\test\" + output.Key + ".csv", output);
}







但是它没有包含拆分文件中的标题,所以我试过这个 -






This worked however it didnt include the headers in the split files, so I have tried this -

var headerLine = File.ReadLines(@"C:\test\test1.csv");
            var nHeaderLine = headerLine.Take(1);

            string FHeader = nHeaderLine.ToString();


            var splitQuery = from line in headerLine
                             let source = line.Split(',').Last()
                             group line by source into outputs
                             select outputs;

            foreach (var output in splitQuery)
            {
                var Foutput = FHeader + "\r\n" + output;

                File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);

            }







我可能这样做完全错了但我得到两个错误(到目前为止),这些是








Im probably doing this completely wrong but im getting two errors (so far) and these are


File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);







Error	1	The best overloaded method match for 'System.IO.File.WriteAllLines(string, System.Collections.Generic.IEnumerable<string>)' has some invalid arguments	C:\Users\nicoles\Documents\Visual Studio 2013\Projects\splitTest\splitTest\Program.cs	32	17	splitTest










Error	2	Argument 2: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>'	C:\Users\nicoles\Documents\Visual Studio 2013\Projects\splitTest\splitTest\Program.cs	32	71	splitTest







有人可以帮帮我吗



我尝试过:






Could someone help me out please

What I have tried:

var headerLine = File.ReadLines(@"C:\test\test1.csv");
            var nHeaderLine = headerLine.Take(1);

            string FHeader = nHeaderLine.ToString();


            var splitQuery = from line in headerLine
                             let source = line.Split(',').Last()
                             group line by source into outputs
                             select outputs;

            foreach (var output in splitQuery)
            {
                var Foutput = FHeader + "\r\n" + output;

                File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);

            }

推荐答案

我最近发布了一篇关于解析CSV文件的文章。它将列名称保存在名为列的单独列表中。该类非常轻量级,您可以继承该类并覆盖几乎所有功能,以执行您需要它执行的操作。



CSV文件解析器 [ ^ ]



它不写文件,因为它的主要目的是只需读取CSV文件并将字段分成各自的部分即可。但是,在派生类中,您可以确定添加该功能。
I recently posted an article about parsing CSV files. It maintains the column names in a separate list called "Columns". The class is pretty lightweight and you can inherit the class and override pretty much all of the functionality to do what you need it to do.

CSV File Parser[^]

It doesn't write files, because it's primary purpose is to simply read CSV files and separate the fields into their respective parts. However, in your derived class, you can certain add that functionality.


WriteAllLines 需要收集字符串作为输入,因此你需要创建一个字符串集合,其中包含需要保存在文件中的所有行,然后在最后调用WriteAllLines方法写入它只需一次:



WriteAllLines need collection of string as input, so you need to create a collection of string that contains all lines that needs to be saved in file and then at last call the WriteAllLines method write it just once like:

var lines=  splitQuery.Select(output=> FHeader + "\r\n" + output)
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", lines);           





或更高可以通过以下方式避免额外的linq查询:





or more better can be to avoid extra linq query by doing :

var lines = from line in headerLine
            let source = line.Split(',').Last()
            group line by source into outputs
            select FHeader + "\r\n" + outputs;

File.WriteAllLines(@"C:\test\" + output.Key + ".csv", lines); 


参见 File.WriteAllLines方法(System.IO) [ ^ ];它没有带两个字符串的重载。你真的想在输出中的每个文本行之前写下标题行吗?尝试使用适当的变量类型,而不是在任何地方使用 var ,并且可能更容易看出错误。
See File.WriteAllLines Method (System.IO)[^]; it does not have an overload that takes two strings. And do you really want to write the header line before every text line in your output? Try using proper variable types, rather than using var everywhere, and it is probably easier to see what is wrong.


这篇关于使用标题拆分CSV文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆