2016-08-10 34 views
0

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

任何建议将大大升值。

static void Main(string[] args) 
    { 
     var filePath = Path.Combine(Directory.GetCurrentDirectory(), "InventoryReport 02_08_2016.csv"); 
     var fileContents = ReadFile(filePath); 
     foreach (var line in fileContents) 
     { 
      Console.WriteLine(line); 
     } 

     Console.WriteLine("Press any key to exit..."); 
     Console.ReadKey(); 
    } 

    public static IList<string> ReadFile(string fileName) 
    { 
     var results = new List<string>(); 

     var target = File 
.ReadAllLines(fileName) 
.Skip(1) // Skip the line with column names 
.Select(line => line.Replace(' ', ',')); // ... splitting pattern 

     // Writing back to some other file 
     File.WriteAllLines(fileName, target); 

     return results; 
    } 
} 

回答

0

确定你清楚应用该替换对整个文件,这就是为什么它会影响所有的列,尝试另一种方法:使用逗号

var lineElements = line.split(','); 

-loop每行
-in每次循环裂元件

- 然后具体应用,为第5列:

var fifthElement = lineElements[4].Replace(' ', ','); 

- 将要只适用于第5个元素
-combine它的效果,并得到全行你

+0

哪里会ip系带循环。已经得到了一些代码示例 – user5813072

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我意识到你必须跳过第一行,因为我有更好的代码才能更新代码并同时解决你的问题。

此选项写入文件头记录的,是

/// <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 
+0

感谢您的代码,我认为这将解决我的解决方案,但方法已创建后。现在不会有数据有7列,因为一旦插入逗号,它会将以下数据推入新列? – user5813072

+0

确实如此。实际上,标题记录和记录本身之间现在存在不匹配。标题记录现在比记录少1列。这很容易避免,尽管我不确定你的意图是什么,所以我没有把它放到示例代码中。你的问题在感觉上有点模棱两可,我不知道你是否想在该领域添加一个逗号并保留一个字段,或者如果你打算将字段实际分割成2列,这需要一点点更多代码来调整标题记录。 –

+0

@ user5813072,在执行代码的第三部分时,我发现我的“开始数据”在标题记录和记录本身之间已经有一个列计数不匹配。记录中还有一列。尽管代码片段的原理仍然相同。我会用处理标题记录的这个新版本暂时更新我怀疑的答案。 –

相关问题