【问题标题】:Delete columns from CSV then save in different folder path从 CSV 中删除列,然后保存在不同的文件夹路径中
【发布时间】:2019-04-30 23:21:30
【问题描述】:

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

由于稍后将与此集成其他脚本,我决定使用 C# 来执行此操作。

我以前使用过 Perl 并达到了预期的效果,但它不适合我的应用程序。

我找到了这个link,在那里我找到了这段代码;

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"));
    }
}
}

这使用 C# 打开一个 CSV 文件,对其进行操作并将其保存在新路径中。

我想从 csv 文件中删除第 0、1、2 和 3 列,而不是处理数据。

谁能帮忙?

【问题讨论】:

    标签: c# csv export-to-csv


    【解决方案1】:

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

    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();
    }
    

    更新:

    要排除第 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();
    }
    

    【讨论】:

    • 我刚刚做了这个修改并达到了预期的结果,只是出于好奇,如果我也想删除第 10 列,我该怎么做?
    • 您必须在内部循环中添加 if 导致跳过该列。
    • 这会在if (col &lt; _cols - 1) { stream.Write(delim); } 子句中吗?
    猜你喜欢
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多