如何使用C#处理CSV文件中的换行符? [英] How do I handle line breaks in a CSV file using C#?

查看:491
本文介绍了如何使用C#处理CSV文件中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel电子表格在C#转换为CSV文件,但我有一个问题,处理换行符。例如:

 John,23,555-5555

Peter ,24,555-5
555

Mary,21,555-5555

当我读取CSV文件时,如果记录不是以双引号()开头,那么换行符是错误的,我必须删除它。



我应该如何处理这些换行符?






非常感谢大家的帮助。



远,我的记录有固定格式,所有的开头都是
JTW; ...; ....; ...;



JTW; ... ; ...; ....



JTW; ....; ...; p> ..; ...; ...(错误的记录,插入了行制动)



JTW; ...; ...



所以我检查了;在每行的[3]位置。如果真的我写,如果错误的最后一个*删除linebrak)



Im现在有问题,因为im将文件保存为一个txt。



顺便说一句,我将excell spreadshit转换为csv,并保存为excel中的csv。但我不知道客户端是否这样做。



所以文件作为TXT是完美的。检查记录和总计。但现在我不得不把它转换回csv和我真的想在程序中做它。有人知道吗?



这里是我的代码:



命名空间EditorCSV
{
class Program
{
static void Main(string [] args)
{
ReadFromFile(c:\source.csv);
}

  static void ReadFromFile(string filename)
{
StreamReader SR;
StreamWriter SW;
SW = File.CreateText(c:\\target.csv);
string S;
char C ='a';
int i = 0;
SR = File.OpenText(filename);
S = SR.ReadLine();
SW.Write(S);
S = SR.ReadLine();
while(S!= null)
{
try {C = S [3]; }
catch(IndexOutOfRangeException exception){
bool t = false;
while(t == false)
{
t = true;
S = SR.ReadLine();
try {C = S [3]; }
catch(IndexOutOfRangeException ex){S = SR.ReadLine(); t =假; }

}
}
if(C.Equals(';'))
{
SW.Write(\r\\\
+ S);
i = i + 1;
}
else
{
SW.Write(S);

}
S = SR.ReadLine();
}
SR.Close();
SW.Close();
Console.WriteLine(Records Processed:+ i.ToString()+。
Console.WriteLine(File Created SucacessFully);
Console.ReadKey();


}




}
}


解决方案

CSV有预定义的处理方式。本网站提供了标准处理CSV所有注意事项的方式的简单说明



然而,没有理由不使用坚实的开源库来读写CSV文件,以避免出现非标准的错误。 LINQtoCSV 是我最喜欢的图书馆。



或者,这个SO问题CSV库将给你最流行的选择的列表。


I have an Excel spreadsheet being converted into a CSV file in C#, but am having a problem dealing with line breaks. For instance:

"John","23","555-5555"

"Peter","24","555-5
555"

"Mary,"21","555-5555"

When I read the CSV file, if the record does not starts with a double quote (") then a line break is there by mistake and I have to remove it. I have some CSV reader classes from the internet but I am concerned that they will fail on the line breaks.

How should I handle these line breaks?


Thanks everybody very much for your help.

heres is what ive done so far, my records have fixed format and all start with JTW;...;....;...;

JTW;...;...;....

JTW;....;...;..

..;...;... (wrong record, line brak inserted)

JTW;...;...

so i checked for the ; in the [3] position of each line. if true i write, if false ill apend on the last *removing the linebrak)

Im having problems now because im saving the file as a txt.

By the way, i am converting the excell spreadshit to csv by saving as csv in excell. but im not sure if the client is doing that.

So the file as a TXT is perfect. ive checked the records and totals. But now i have to convert it back to csv and i would really like to do it in the program. Does anybody know how ?

here is my code:

namespace EditorCSV { class Program { static void Main(string[] args) { ReadFromFile("c:\source.csv"); }

    static void ReadFromFile(string filename)
    {
        StreamReader SR;
        StreamWriter SW;
        SW = File.CreateText("c:\\target.csv");
        string S;
        char C='a';
        int i=0;
        SR=File.OpenText(filename);
        S=SR.ReadLine();
        SW.Write(S);
        S = SR.ReadLine();
        while(S!=null)
        {
            try { C = S[3]; }
            catch (IndexOutOfRangeException exception){
                bool t = false;
                while (t == false)
                {
                    t = true;
                    S = SR.ReadLine();
                    try { C = S[3]; }
                    catch (IndexOutOfRangeException ex) { S = SR.ReadLine(); t = false; }

                }
            }
            if( C.Equals(';'))
            {
                SW.Write("\r\n" + S);
                i = i + 1;
            }
            else
            {
                SW.Write(S);

            }
            S=SR.ReadLine();
        }
        SR.Close();
        SW.Close();
        Console.WriteLine("Records Processed: " + i.ToString() + " .");
        Console.WriteLine("File Created SucacessFully");
        Console.ReadKey();


    }




    }
} 

解决方案

CSV has predefined ways of handling that. This site provides an easy to read explanation of the standard way to handle all the caveats of CSV.

Nevertheless, there is really no reason to not use a solid, open source library for reading and writing CSV files to avoid making non-standard mistakes. LINQtoCSV is my favorite library for this. It supports reading and writing in a clean and simple way.

Alternatively, this SO question on CSV libraries will give you the list of the most popular choices.

这篇关于如何使用C#处理CSV文件中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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