更新CSV文件C#一列 [英] Update one column in CSV File C#
问题描述
我想根据从CSV文件数据库状态更新一列(第三列)。
想与,,与部门1500 CSV删除100,并更换它雇员文件
数据库表:
的EmpID DEPTID100 1500101 1300102 1500
CSV文件,如图象下面这样:
的EmpID,EmpName,分数,Grade1,Grade2,Grade3
100,emp1,100,A1,A3
101,emp2,250,A1,A5,A2
102,emp3,100,A1
结果应该是这样的:
100,EMP1,A1,A3
101,emp2,250,A1,A5,A2
102,EMP3,A1
第一件事,我无法取代第三列中的值,请参见下面的code:
字符串文件1 = @F:\\ test.csv字符串[] =行System.IO.File.ReadAllLines(文件1);
System.IO.StreamWriter SW =新System.IO.StreamWriter(文件1);
的foreach(在行字符串s){
sw.WriteLine(s.Replace(100,));
}
sw.Close();
如果我给在foreach循环下面一行:
sw.WriteLine(Regex.Replace(S,s.Split(,)[2],M => s.Split(,)[2]。 。的ToString()更换(100,)));
这是从100更换所有的值空字符串。
你能告诉我如何在第3列,以取代的价值?
先谢谢了。
VAR线=新的字符串[10];
变种splitLines = lines.Select(L => l.Split(,));
的foreach(在splitLines VAR splitLine)
{
如果(splitLine [2] ==100)
{
splitLine [2] =0;
}
变种线=的string.join(,,splitLine); //然后做你希望与什么样线。
}
I want to update one column (3rd column) based on condition from database in CSV File. Want to Remove 100, and replace it with ,, for the Employees with Dept 1500 in CSV File
Database table:
EmpID DeptID
100 1500
101 1300
102 1500
CSV File as shown like below:
EmpID,EmpName,Score,Grade1,Grade2,Grade3
100,emp1,100,A1,A3
101,emp2,250,A1,A5,A2
102,emp3,100,A1
Result should be like this:
100,emp1,,A1,A3
101,emp2,250,A1,A5,A2
102,emp3,,A1
First thing, I am unable to replace the values in third column, see the code below:
string file1 = @"F:\test.csv"; string[] lines = System.IO.File.ReadAllLines(file1);
System.IO.StreamWriter sw = new System.IO.StreamWriter(file1);
foreach(string s in lines) {
sw.WriteLine(s.Replace("100", ""));
}
sw.Close();
If i give the below line in foreach loop:
sw.WriteLine(Regex.Replace(s, s.Split(',')[2], m => s.Split(',')[2].ToString().Replace("100", "")));
It is replacing all the values from 100 to empty string.
Can you please tell me how to replace the value in the 3rd column?
Thanks in Advance.
var lines = new string[10];
var splitLines = lines.Select(l => l.Split(','));
foreach (var splitLine in splitLines)
{
if (splitLine[2] == "100")
{
splitLine[2] = "0";
}
var line = string.Join(",", splitLine);
// And then do what you wish with the line.
}
这篇关于更新CSV文件C#一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!