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

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

问题描述

我有一个Excel电子表格转换成C#中的CSV文件,但我有处理断行的问题。例如:

 约翰,23,555-5555

彼得,24,555-5
555

玛丽,21,555-5555

当我读了CSV文件,如果记录不以一个双引号()将启动,然后换行有错,我必须将其删除。我从互联网上一些CSV读卡器类,但我担心他们会在换行失败。



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






谢谢大家非常的帮助。



继承人是香港专业教育学院这样做到目前为止,我的记录已经固定的格式和所有先从
JTW; ...; ...; ...;



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



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



..; ...; ...(错误的记录,行BRAK插)



JTW; ...; ...



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



即时通讯有问题,因为现在的IM将文件保存为一个txt。



顺便说一句,我是EXCELL spreadshit通过保存在EXCELL CSV转换为csv。但林不知道如果客户这样做。



所以文件为TXT是完美的。香港专业教育学院检查记录和汇总。但现在我必须把它转换回CSV和我真的想这样做的计划。有谁知道怎么样?



下面是我的代码:



命名空间EditorCSV
{
类节目
{
静态无效的主要(字串[] args)
{
ReadFromFile(C:\source.csv);
}

 静态无效ReadFromFile(字符串文件名)
{
StreamReader的SR;
StreamWriter的SW;
SW = File.CreateText(C:\\target.csv);
串S;
字符C ='A';
INT I = 0;
SR = File.OpenText(文件名);
S = SR.ReadLine();
SW.Write(S);
S = SR.ReadLine();
,而(!S = NULL)
{
尝试{C = S [3]; }
赶上(IndexOutOfRangeException除外){
布尔T = FALSE;
,而(T ==假)
{
T =真实的;
S = SR.ReadLine();
尝试{C = S [3]; }
赶上(IndexOutOfRangeException前){S = SR.ReadLine(); T = FALSE; }

}
}
如果(C.Equals(';'))
{
SW.Write(\r\\\
+ S);
I = I + 1;
}
,否则
{
SW.Write(S);

}
S = SR.ReadLine();
}
SR.Close();
SW.Close();
Console.WriteLine(已处理的记录:+ i.ToString()+。);
Console.WriteLine(文件创建SucacessFully);
Console.ReadKey();


}




}
}


解决方案

CSV预定义了处理方式的。该网站提供了一个易于阅读的标准的方式来处理CSV 的所有注意事项的说明。



不过,实在没有理由不使用阅读了坚实的,开放源码库和写入CSV文件,以避免不规范的错误。 LINQtoCSV 是我最喜欢这个库。它支持阅读和在一个清洁,简单的方式写作。



另外,的这对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天全站免登陆