您当前的方式影响整个线路(.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
哪里会ip系带循环。已经得到了一些代码示例 – user5813072