如何将更改应用于CSV文件中的特定列?

时间:2016-08-10 12:47:00

标签: c# .net csv

在我的CSV文件中,我有一个包含字符串和日期时间类型的列。这是我的CSV文件中包含此数据的第5列(TUI 01/01/01)。 我对空格中的逗号应用了一些更改,以便在两个不同的列(TUI,01 / 01/01)中分隔此值。但是,这些更改会影响其值之间有空格的其他列数据。 我只是想应用这些更改只影响第五列。

任何建议都会非常值得赞赏。

$("#description").height()

2 个答案:

答案 0 :(得分:0)

好的,你明确地将替换应用于整个文件,这就是它影响所有列的原因,尝试另一种方式:
-loop每一行
-in每个循环使用逗号分割元素

UIImagePicker

- 然后特别将其应用于第5栏:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'  
}

- 仅对第5个元素施加效果
- 将它组合起来并按照你的要求获得整行

答案 1 :(得分:0)

您当前的方式影响整行(.Select(line => line.Replace(' ', ','))),而不仅仅是行上的第5个字段。这是另一种方式。

选项1

    public Form1()
    {
        InitializeComponent();
        ReWriteFile(@"M:\StackOverflowQuestionsAndAnswers\38873875\38873875\testdata.csv");//call the method
    }

    public void ReWriteFile(string fileName)
    {
        using (StreamWriter sw = new StreamWriter(@"M:\StackOverflowQuestionsAndAnswers\38873875\38873875\testdata_new.csv"))
        {
            string currentLine = string.Empty;
            using (StreamReader sr = new StreamReader(fileName))//
            {
                while ((currentLine = sr.ReadLine()) != null)//while there are lines to read
                {
                    string[] fielded = currentLine.Split(',');//split your fields into an array
                    fielded[4] = fielded[4].Replace(" ", ",");//replace the space in position 4(field 5) of your array
                    sw.WriteLine(string.Join(",", fielded));//write the line in the new file
                }
            }
        }

    }

我的起始数据是这样的(列数不匹配,但它没有保留任何工作,因为它应该用于此问题的目的):

col1,col2,col3,col4,col5,col6
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data

经过这个方法之后就变成了这个:

col1,col2,col3,col4,col5,col6
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data

在这里阅读了你的问题之后 how to display the column names on my csv file 我意识到你必须跳过第一行是有充分理由的,所以我更新了我的代码并解决了你的问题' s问题同时存在。

此选项按原样写入标题记录

    /// <summary>
    /// In this method I use the lineCounter as my position tracker to know which line I'm readin at the time
    /// </summary>
    /// <param name="fileName"></param>
    public void ReWriteFileDontAffectFirstRow(string fileName)
    {
        int lineCounter = 0;//lets make our own position tracker
        using (StreamWriter sw = new StreamWriter(@"M:\StackOverflowQuestionsAndAnswers\38873875\38873875\testdata_new2.csv"))
        {
            string currentLine = string.Empty;
            using (StreamReader sr = new StreamReader(fileName))
            {
                while ((currentLine = sr.ReadLine()) != null)//while there are lines to read
                {
                    if (lineCounter != 0)
                    {
                        //If it's not the first line
                        string[] fielded = currentLine.Split(',');//split your fields into an array
                        fielded[4] = fielded[4].Replace(" ", ",");//replace the space in position 4(field 5) of your array
                        sw.WriteLine(string.Join(",", fielded));//write the line in the new file
                    }
                    else
                    {
                        //If it's the first line
                        sw.WriteLine(currentLine);//Write the line as-is
                    }
                    lineCounter++;
                }
            }
        }
    }

原始数据是这样的(列数不匹配,但它不会使任何事情无法正常工作

col 1,col 2,col 3,col 4,col 5,col 6
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data,col 6 data

完成方法后的数据

col 1,col 2,col 3,col 4,col 5,col 6
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data,col 6 data

此选项会在标题记录中添加一个字段

    public void ReWriteFileAdjustHeaderRecordToo(string fileName)
    {
        int lineCounter = 0;//lets make our own position tracker
        using (StreamWriter sw = new StreamWriter(@"M:\StackOverflowQuestionsAndAnswers\38873875\38873875\testdata_new3.csv"))
        {
            string currentLine = string.Empty;
            using (StreamReader sr = new StreamReader(fileName))
            {
                while ((currentLine = sr.ReadLine()) != null)//while there are lines to read
                {
                    List<string> fielded = new List<string>(currentLine.Split(','));//splits your fields into a list of fields. This is no longer a string[]. Its a list so we can "inject" the new column in the header record.
                    if (lineCounter != 0)
                    {
                        //If it's not the first line
                        fielded[4] = fielded[4].Replace(" ", ",");//replace the space in position 4(field 5) of your array
                        sw.WriteLine(string.Join(",", fielded));//write the line in the new file
                    }
                    else
                    {
                        //If it's the first line
                        fielded.Insert(4, "new inserted col");//inject the new column into the list
                        sw.WriteLine(string.Join(",", fielded));//write the line in the new file
                    }
                    lineCounter++;
                }
            }
        }
    }

启动数据(此数据具有正确的起始列数

col 1,col 2,col 3,col 4,col 5,col 6
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI 01/01/01,col 5 data

方法后的数据

col 1,col 2,col 3,col 4,new inserted col,col 5,col 6
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data
col 1 data,col 2 data,col 3 data,col 4 data,TUI,01/01/01,col 5 data