如何逐行读取csv文件及其字段。

如何逐行读取csv文件及其字段。

问题描述:

我是C#初学者刚刚开始编码。

我想逐行读取csv文件及其字段。然后通过从文本框添加数据将其写入文本文件(1new.txt)。



我尝试过:



I am beginner in C# just started coding.
I want to read csv file line by line and their fields. Then write it to text file (1new.txt) by adding data from text box to it.

What I have tried:

using (TextFieldParser parser = new TextFieldParser(@"c:\\barcode\\brcd4.csv"))
                {

                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    // parser.SetDelimiters("|");



                    //string[] fields; // Used to hold the fields after each read




                    {
                        string[] fields = parser.ReadFields(); // Parse the line just read into the array

                        // foreach (var field in fields)         // Process the fields

                        List<string[]> listOfValues = new List<string[]>();
                        using (var fs = File.OpenRead(@"c:\\barcode\\brcd4.csv"))
                        using (var read = new StreamReader(fs))
                        {
                            while (!read.EndOfStream)
                            {

                                int i = 0;
                                var line = read.ReadLine();
                                while (i <= 10)
                                {
                                    var values = line.Split(',');

                                    // var line = read.ReadLine();
                                     //listOfValues.Add(line.Split(','));



                                    //string[] fields = parser.ReadFields();


                                    //using (var fields = new StreamReader(@"c:\\barcode\\brcd4.csv"))


                                    //var fields = sw1.GetField<int>(0);

                                    sw1.WriteLineAsync(textBox1.Text + "" + fields[0]);
                                    sw1.WriteLineAsync(textBox2.Text + "" + fields[1]);
                                    sw1.WriteLineAsync(textBox3.Text + "" + fields[2]);
                                    sw1.WriteLineAsync(textBox4.Text + "" + fields[3]);
                                    sw1.WriteLineAsync(textBox5.Text + "" + fields[4]);
                                    sw1.WriteLineAsync(textBox6.Text + "" + fields[5]);
                                    sw1.WriteLineAsync(textBox1.Text + "" + fields[6]);
                                    //sw1.WriteLineAsync(textBox2.Text + "" + fields[7]);
                                    //sw1.WriteLineAsync(textBox3.Text + "" + fields[8]);
                                    //sw1.WriteLineAsync(textBox4.Text + "" + fields[9]);
                                    //sw1.WriteLineAsync(textBox5.Text + "" + fields[10]);
                                    //sw1.WriteLineAsync(textBox6.Text + "" + fields[11]);


                                    //Console.Write(values[i]);
                                    //Console.Read();
                                    i++;
                                    // {
                                       Console.Write(values + "  |  ");
                                    //  }
                                    // Console.WriteLine();
                                }

                            }
                        }
File.WriteAllText(@"c:\barcode\1new.txt", sw1.ToString());

最好的方法是使用现有的包 - 快速CSV阅读器 [ ^ ]是我使用的那个 - 因为它并不像你想象的那么简单。请记住,字符串可以包含逗号和双引号,因此在逗号分割和修剪双引号时并不是一个简单的例子 - 您必须进行一些智能处理。
The best way is to use an existing package - A Fast CSV Reader[^] is the one I use - as it isn't as simple as you might think. Remember, strings can contain commas and double quotes, so it's isn't a trivial case of splitting on commas and trimming off double quotes - you have to do some intelligent processing.


解决方案#1由OriginalGriff非常好。除此之外,我还可以推荐另一个 CSV文件解析器 [ ^ ]



作为替代方案,您可以使用 ADO.NET OleDb [ ^ ]。请阅读这些文章:

关于文本文件的大量ADO | Microsoft Docs [ ^ ]

Schema.ini文件(文本文件驱动程序) )| Microsoft Docs [ ^ ]

如何:向文本文件数据源添加架构定义 [ ^ ]

文本文件连接字符串 - ConnectionStrings.com [ ^ ]

使用OleDb导入文本文件(选项卡,CSV ,自定义) [ ^ ]

读取逗号分隔文件 [ ^ ]

读取文本文件(txt,csv,日志,制表符,固定长度) [ ^ ]



请查看我过去的答案以获取更多详细信息(示例代码):读取文本文件特定列 [ ^ ]



祝你好运!
Solution #1 by OriginalGriff is very good. In addition to it, i can recommend another CSV File Parser[^]

As an alaternative you can use ADO.NET OleDb[^]. Please, read these articles:
Much ADO About Text Files | Microsoft Docs[^]
Schema.ini File (Text File Driver) | Microsoft Docs[^]
How to: Add a Schema Definition to a Text File Data Source[^]
Textfile connection strings - ConnectionStrings.com[^]
Using OleDb to Import Text Files (tab, CSV, custom)[^]
Reading comma delimited files[^]
Read Text File (txt, csv, log, tab, fixed length)[^]

Please, check out my past answers to get more details (example code): Read Text File Specific Columns[^]

Good luck!