如何在C#控制台中为csv文件创建新列 [英] How to create a new column in C# console for csv file

查看:102
本文介绍了如何在C#控制台中为csv文件创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法从csv文件中获取所有行和列。我不确定file.createtext和file.appendtext之间的区别。对不起,问题仍然是羚牛文凭。这是我的代码。



我尝试了什么:



< pre lang =c#> class 计划
{
静态 void Main( string [] args)
{
string path = Accounts.csv;

// 此文本仅添加一次到文件中。


// 打开要读取的文件。
string [] readText = File.ReadAllLines(path);
while true
{
Console.Write(< span class =code-string> 输入第一个字段或q退出:);
string first = Console.ReadLine();
if (first == q || first == Q返回;
bool found = false ;

foreach string s in readText)
{
if (s.StartsWith(first + ))
{
string [] fields = s .Split(' ,');
Console.WriteLine( 第二个字段是:{0},字段[< span class =code-digit> 1
]);
Console.WriteLine( 第三个字段是:{0},字段[< span class =code-digit> 2 ]);
Console.WriteLine( 第四个字段是:{0},字段[< span class =code-digit> 3 ]);
Console.WriteLine( firth字段为:{0},字段[< span class =code-digit> 4 ]);
Console.WriteLine( 第三个字段是:{0},字段[< span class =code-digit> 5 ]);
Console.WriteLine( 第六个字段是:{0},字段[< span class =code-digit> 6 ]);
Console.WriteLine( 第七个字段是:{0},字段[< span class =code-digit> 7 ]);
Console.WriteLine( eigth字段为:{0},字段[< span class =code-digit> 8 ]);
Console.WriteLine( 第九个字段是:{0},字段[< span class =code-digit> 9 ]);



found = true ;
break ;
}
}
if (!found)Console.WriteLine( 没有第一个字段为{0}的记录,第一个);
Console.WriteLine();
}
}
}

解决方案

我不想这样说,但......那个代码非常糟糕,并不适合你的问题。

第一个问题是你依赖文件完全正确 - 这很危险。

如果每一行不包含10列,则会失败。

如果任何列包含逗号(例如地址经常这样做),那么您的数据将会关闭。



首先检查Split的返回值:它是否有正确的列数?然后考虑用双引号将CSV文件中的字符串括起来:

Mike Smith,Junior,123,A Street,A town,17899,... 

它需要更复杂的处理来阅读它,但它更可靠,以后会为你节省很多心痛。



As关于File.CreateText和File.AppendText最简单的方法就是查看文档:

File.CreateText Method(String)(System.IO) [ ^ ]

File.AppendText Method(String)(System。 IO) [ ^ ]

基本上,第一个抛弃现有文件以支持新数据,第二个将新数据添加到现有文件的末尾。

(但你应该可以从名字中猜出来!)


i manage to fetch the all the row and column from the csv file. im not sure whats the difference between file.createtext and file.appendtext. sorry for the question im 18 still takin diploma. this is my code.

What I have tried:

class Program
    {
        static void Main(string[] args)
        {
            string path = "Accounts.csv";

            // This text is added only once to the file.


            // Open the file to read from.
            string[] readText = File.ReadAllLines(path);
            while (true)
            {
                Console.Write("Enter first field or 'q' to quit : ");
                string first = Console.ReadLine();
                if (first == "q" || first == "Q") return;
                bool found = false;

                foreach (string s in readText)
                {
                    if (s.StartsWith(first + "," ))
                    {
                        string[] fields = s.Split(',');
                        Console.WriteLine("The second field is : {0}", fields[1]);
                        Console.WriteLine("The third field is : {0}", fields[2]);
                        Console.WriteLine("The forth field is : {0}", fields[3]);
                        Console.WriteLine("The firth field is : {0}", fields[4]);
                        Console.WriteLine("The third field is : {0}", fields[5]);
                        Console.WriteLine("The sixth field is : {0}", fields[6]);
                        Console.WriteLine("The seventh field is : {0}", fields[7]);
                        Console.WriteLine("The eigth field is : {0}", fields[8]);
                        Console.WriteLine("The ninth field is : {0}", fields[9]);
                   


                        found = true;
                        break;
                    }
                }
                if (!found) Console.WriteLine("There is no record with a first field of {0}", first);
                Console.WriteLine();
            }
        }
    }

解决方案

I hate to say this, but ... that code is pretty terrible, and doesn't really fit your question.
The first problem is that you are relying on the file being exactly correct - and that is dangerous.
If each row does not contain 10 columns, it will fail.
if any column contains a comma (as for example addresses often do) then your data will be off.

Start by checking the return value from Split: does it have the right number of columns? Then consider enclosing strings within your CSV file in double quotes:

"Mike Smith, Junior", "123, A Street, A town", 17899, ...

It requires more complex processing to read it, but it is considerably more reliable and will save you a lot of heartache later on.

As regards File.CreateText and File.AppendText the simplest way to find out teh difference is to look at the documentation:
File.CreateText Method (String) (System.IO)[^]
File.AppendText Method (String) (System.IO)[^]
Basically, the first throws away the existing file in favour of the new data, the second adds new data to the end of an existing file.
(But you should have been able to guess that from the names!)


这篇关于如何在C#控制台中为csv文件创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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