C# 从CSV中删除列,然后保存在不同的文件夹路径中

C# 从CSV中删除列,然后保存在不同的文件夹路径中,c#,csv,export-to-csv,C#,Csv,Export To Csv,我需要一个脚本,从特定文件路径打开CSV,删除特定列,然后将CSV导出到指定文件夹 由于其他脚本将在稍后阶段与此集成,因此我决定使用C#来实现这一点 我以前使用过Perl并获得了预期的结果,但它不适合我的应用程序 我在找到代码的地方找到了这个 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CSV { class CS

我需要一个脚本,从特定文件路径打开CSV,删除特定列,然后将CSV导出到指定文件夹

由于其他脚本将在稍后阶段与此集成,因此我决定使用C#来实现这一点

我以前使用过Perl并获得了预期的结果,但它不适合我的应用程序

我在找到代码的地方找到了这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CSV
{
class CSV
{
    private Dictionary<Tuple<int, int>, string> _data;
    private int _rows;
    private int _cols;

    public int Rows { get { return _rows; } }
    public int Cols { get { return _cols; } }

    public CSV()
    {
        Clear();
    }

    public void Clear()
    {
        _rows = 0;
        _cols = 0;
        _data = new Dictionary<Tuple<int, int>, string>();
    }

    public void Open(StreamReader stream, char delim = ',')
    {
        string line;
        int col = 0;
        int row = 0;

        Clear();

        while ((line = stream.ReadLine()) != null)
        {
            if (line.Length > 0)
            {
                string[] values = line.Split(delim);
                col = 0;
                foreach (var value in values)
                {
                    this[col, row] = value;
                    col++;
                }
                row++;
            }
        }
        stream.Close();
    }

    public void Save(StreamWriter stream, char delim = ',')
    {
        for (int row = 0; row < _rows; row++)
        {
            for (int col = 0; col < _cols; col++)
            {
                stream.Write(this[col, row]);
                if (col < _cols - 1)
                {
                    stream.Write(delim);
                }
            }
            stream.WriteLine();
        }
        stream.Flush();
        stream.Close();
    }

    public string this[int col, int row]
    {
        get
        {
            try
            {
                return _data[new Tuple<int, int>(col, row)];
            }
            catch
            {
                return "";
            }
        }

        set
        {
            _data[new Tuple<int, int>(col, row)] = value.ToString().Trim();
            _rows = Math.Max(_rows, row + 1);
            _cols = Math.Max(_cols, col + 1);
        }
    }

    static void Main(string[] args)
    {
        CSV csv = new CSV();

        csv.Open(new StreamReader(@"C:\mid\Dev\CSV_Splitter\VR_Path\New\Import_User_Sample_en.csv"));


        csv[0, 0] = "Column0";
        csv[1, 1] = "100";
        csv[2, 2] = "200";
        csv[3, 3] = "300";
        csv[4, 4] = "400";




csv.Save(new StreamWriter(@"C:\mid\Dev\CSV_Splitter\VR_Path\Proccessed\test_out.csv"));
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
名称空间CSV
{
类CSV
{
私有字典(private Dictionary)数据;;
私有整数行;
私人国际公司;
公共int行{get{return\u Rows;}}
public int Cols{get{return\u Cols;}}
公共CSV()
{
清除();
}
公共空间清除()
{
_行=0;
_cols=0;
_数据=新字典();
}
公共void Open(StreamReader流,char delim=',')
{
弦线;
int col=0;
int行=0;
清除();
而((line=stream.ReadLine())!=null)
{
如果(直线长度>0)
{
字符串[]值=行分割(delim);
col=0;
foreach(值中的var值)
{
此[列,行]=值;
col++;
}
行++;
}
}
stream.Close();
}
public void Save(StreamWriter流,char delim=',')
{
对于(int行=0;行<_行;行++)
{
for(int col=0;col<\u cols;col++)
{
stream.Write(这个[col,row]);
如果(列<\u列-1)
{
stream.Write(delim);
}
}
stream.WriteLine();
}
stream.Flush();
stream.Close();
}
公共字符串this[int col,int row]
{
得到
{
尝试
{
返回_数据[新元组(列,行)];
}
抓住
{
返回“”;
}
}
设置
{
_数据[新元组(列,行)]=value.ToString().Trim();
_行=数学最大值(_行,行+1);
_cols=Math.Max(_cols,col+1);
}
}
静态void Main(字符串[]参数)
{
CSV=新的CSV();
打开(新的StreamReader(@“C:\mid\Dev\csv\u Splitter\VR\u Path\new\Import\u User\u Sample\u en.csv”);
csv[0,0]=“Column0”;
csv[1,1]=“100”;
csv[2,2]=“200”;
csv[3,3]=“300”;
csv[4,4]=“400”;
保存(新StreamWriter(@“C:\mid\Dev\csv\u Splitter\VR\u Path\processed\test\u out.csv”);
}
}
}
这将使用C#打开CSV文件,对其进行操作并将其保存在新路径中

我想从csv文件中删除列0、1、2和3,而不是操作数据


有人能帮忙吗?

您可以使用发布的代码,只需更改
Save
方法,用4初始化col变量,就可以了

public void Save(StreamWriter stream, char delim = ',')
{
    if(_cols > 4) 
    {
    for (int row = 0; row < _rows; row++)
    {
        for (int col = 4; col < _cols; col++)
        {
            stream.Write(this[col, row]);
            if (col < _cols - 1)
            {
                stream.Write(delim);
            }
        }
        stream.WriteLine();
    }
    }
    stream.Flush();
    stream.Close();
}
public void Save(StreamWriter流,char delim=',')
{
如果(_cols>4)
{
对于(int行=0;行<_行;行++)
{
for(int col=4;col<\u cols;col++)
{
stream.Write(这个[col,row]);
如果(列<\u列-1)
{
stream.Write(delim);
}
}
stream.WriteLine();
}
}
stream.Flush();
stream.Close();
}
更新:

要排除第10列,请跳过在第10个位置写入数据

public void Save(StreamWriter stream, char delim = ',')
{
    if(_cols > 4) 
    {
    for (int row = 0; row < _rows; row++)
    {
        for (int col = 4; col < _cols; col++)
        {
            if(col != 10)
            {
                stream.Write(this[col, row]);
                if (col < _cols - 1)
                {
                    stream.Write(delim);
                }
            }
        }
        stream.WriteLine();
    }
    }
    stream.Flush();
    stream.Close();
}
public void Save(StreamWriter流,char delim=',')
{
如果(_cols>4)
{
对于(int行=0;行<_行;行++)
{
for(int col=4;col<\u cols;col++)
{
如果(列数=10)
{
stream.Write(这个[col,row]);
如果(列<\u列-1)
{
stream.Write(delim);
}
}
}
stream.WriteLine();
}
}
stream.Flush();
stream.Close();
}

我刚刚做了这个修改并达到了预期的结果,只是出于好奇,如果我希望删除第10列,我将如何继续?您必须添加if-cause-inside-loop来跳过该列。这是否在
if(col<\u cols-1){stream.Write(delim);}
子句中?