C# 使用c删除CSV文件中的列#
我想删除具有特定值的列。下面的代码是我用来删除一行的代码。我可以反转此操作以删除列吗C# 使用c删除CSV文件中的列#,c#,csv,C#,Csv,我想删除具有特定值的列。下面的代码是我用来删除一行的代码。我可以反转此操作以删除列吗 int row = comboBox1.SelectedIndex; string verw = Convert.ToString(txtChange.Text); List<string> lines = new List<string>(); using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(
int row = comboBox1.SelectedIndex;
string verw = Convert.ToString(txtChange.Text);
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(filepath)))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(","))
{
string[] split = line.Split(',');
if (split[row] == kill)
{
//achter split vul je de rij in
}
else
{
line = string.Join(",", split);
lines.Add(line);
}
}
}
}
using (StreamWriter writer = new StreamWriter(path, false))
{
foreach (string line in lines)
writer.WriteLine(line);
}
int row=comboBox1.SelectedIndex;
string verw=Convert.ToString(txtChange.Text);
列表行=新列表();
使用(StreamReader=newstreamreader(System.IO.File.OpenRead(filepath)))
{
弦线;
而((line=reader.ReadLine())!=null)
{
if(第行包含(“,”)
{
string[]split=line.split(',');
如果(拆分[行]==终止)
{
//他在一个星期内分裂了
}
其他的
{
line=string.Join(“,”,split);
行。添加(行);
}
}
}
}
使用(StreamWriter=newstreamwriter(路径,false))
{
foreach(行中的字符串行)
writer.WriteLine(行);
}
假设我们忽略了编写CSV的微妙之处,这应该是可行的:
public void RemoveColumnByIndex(string path, int index)
{
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(path))
{
var line = reader.ReadLine();
List<string> values = new List<string>();
while(line != null)
{
values.Clear();
var cols = line.Split(',');
for (int i = 0; i < cols.Length; i++)
{
if (i != index)
values.Add(cols[i]);
}
var newLine = string.Join(",", values);
lines.Add(newLine);
line = reader.ReadLine();
}
}
using (StreamWriter writer = new StreamWriter(path, false))
{
foreach (var line in lines)
{
writer.WriteLine(line);
}
}
}
public void RemoveColumnByIndex(字符串路径,int索引)
{
列表行=新列表();
使用(StreamReader=新StreamReader(路径))
{
var line=reader.ReadLine();
列表值=新列表();
while(行!=null)
{
value.Clear();
var cols=line.Split(',');
for(int i=0;i
代码实质上加载每一行,将其分解为列,在列中循环,忽略相关列,然后将值放回一行中
当然,这是一种过于简化的方法。我相信还有更有效的方法。要按名称删除该列,这里对您的代码示例进行了一些修改
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(path)))
{
string target = "";//the name of the column to skip
int? targetPosition = null; //this will be the position of the column to remove if it is available in the csv file
string line;
List<string> collected = new List<string>();
while ((line = reader.ReadLine()) != null)
{
string[] split = line.Split(',');
collected.Clear();
//to get the position of the column to skip
for (int i = 0; i < split.Length; i++)
{
if (string.Equals(split[i], target, StringComparison.OrdinalIgnoreCase))
{
targetPosition = i;
break; //we've got what we need. exit loop
}
}
//iterate and skip the column position if exist
for (int i = 0; i < split.Length; i++)
{
if (targetPosition != null && i == targetPosition.Value) continue;
collected.Add(split[i]);
}
lines.Add(string.Join(",", collected));
}
}
using (StreamWriter writer = new StreamWriter(path, false))
{
foreach (string line in lines)
writer.WriteLine(line);
}
列表行=新列表();
使用(StreamReader=newstreamreader(System.IO.File.OpenRead(path)))
{
string target=“;//要跳过的列的名称
int?targetPosition=null;//如果列在csv文件中可用,则这将是要删除的列的位置
弦线;
已收集列表=新列表();
而((line=reader.ReadLine())!=null)
{
string[]split=line.split(',');
收集。清除();
//获取要跳过的列的位置
for(int i=0;i
选择要删除的列的标准是什么?索引?这种情况看起来您已经开始修改代码来做您想做的事情,但是您被卡住了。准确吗?删除列是什么意思?如果可能的话,我想使用的标准是索引或列的名称。是的,我是stuckWell,只要名称与标题匹配,您就会知道不再需要拆分数组的哪个索引…那么我如何删除它?