重新格式化csv文件 [英] reformating csv file

查看:87
本文介绍了重新格式化csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi,
i have a csv file that i want to reformat into a new csv :
source :
H;billythekid;25;male;book;SF;Western Fiction;11/10/2002;;Edition;;Library;
L;302;1;15;paragraph;12
L;412;2;23;paragraph;6
... etc 
and i want that it becomes like that :
H;billythekid;25;male;book      ;SF;Western Fiction       ;11/10/2002;;Edition;;Library;
L;302;1;15        ;paragraph;12
L;412;2;23        ;paragraph;6
i have this code:

try
{
 using (StreamReader sr = new StreamReader(filename)
                {
                 using (StreamWriter sw = new StreamWriter(newfilename)
                        {
                            string line;
                            
                                while ((line = sr.ReadLine())!= null )
                            {
                                if (line.StartsWith("H;"))
                                     {
                                    list = line.Split(';').ToList();
                                    sw.WriteLine(line);
                                   
                                     }
                                     else
                                        { 
                                             list = line.Split(';').ToList();
                                             list.Add(line);
                                             sw.WriteLine(line);
}}}}
catch (Exeption Ex)
{}

推荐答案

我会尝试使用已构建的库来解析 .CSV 文件,因为它们通常具有满足您需要的功能,但如果您打算手动滚动它,那么您可以使用格式字符串 {0,-x} 其中x是列的宽度,例如 {0,-10 }将字符串填充为宽度为10个字符。



之后只需定义每列的宽度并处理文件,这样的事情可能对你有用;



I would try to use an already build library for parsing the .CSV file as they usually come with functionality that does what you need, but if you're intent on hand-rolling this then you can use the format string "{0,-x}" where x is the width of the "column", for example "{0,-10}" pads the string to 10 characters in width.

After that it's just a matter of defining the width of each column and process the file, something like this might work for you;

using System;
using System.Collections.Generic;
using System.IO;

namespace ParseTest {

    class Program  {
        private static bool IsHeader(string line) {
            return line.StartsWith("H;");
        }

        private static void OutputColumns(StreamWriter output, string line, char delimiter, int[] minColumnWidths) {
            IList<string> columns = new List<string>();
            var tokens = line.Split(delimiter);
            for (var i = 0; i < tokens.Length; ++i)
            {
                var formatString = string.Format("{{0,-{0}}}", i < minColumnWidths.Length ? minColumnWidths[i] : 1);
                columns.Add(string.Format(formatString, tokens[i]));
            }

            output.WriteLine(String.Join(delimiter.ToString(), columns));
        }

        public static void Main(string[] args)
        {
            var headerMinWidths = new[] {1, 1, 1, 1, 10, 1, 23, 1, 1, 1, 1, 1};
            var dataMinWidths = new[] {1, 1, 1, 11, 1, 1, 1, 1, 1, 1, 1, 1};
            using(var input = new StreamReader(@"C:\Temp\input.txt")) {
                using (var output = new StreamWriter(@"C:\Temp\output.txt"))
                {
                    string line;
                    while ((line = input.ReadLine()) != null) {
                        OutputColumns(output, line, ';', IsHeader(line) ? headerMinWidths : dataMinWidths);
                    }
                }
            }
        }
    }
}







希望这会有所帮助,

Fredrik




Hope this helps,
Fredrik


不要自己处理CSV文件:它还有更多那。例如,如果一个字段包含分隔符作为文本的一部分,那么它将用双引号括起来,依此类推,这将在一年左右的时间内弄乱你 - 到时为止已经太晚了。 br />
相反,请看一下: C#CSV Reader and Writer [ ^ ] - 它完成它在锡上的说法!



然后你可以使用string.PadRight函数来确保你的CSV值正确间隔。
Don't process the CSV file yourself: there is more to it than that. For example, if a field contains the delimiter character as part of the text, then it will be enclosed in double quotes, and so forth, which will mess you right up in a year or so - by which time it is too late.
Instead, look at this: C# CSV Reader and Writer[^] - it does what it says on the tin!

You can then use the string.PadRight function to make sure your CSV values are correctly spaced.


这篇关于重新格式化csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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