1

I need a script that opens a CSV from a specific file path, deletes specific columns and then exports the CSV to a specified folder.

Due to other scripts that will be integrated with this at a later stage, I have decided to do this using C#.

I have previously used Perl and achieved the desired result, but it isn't suitable for my application.

I found this link where I found this code;

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

This uses C# to open a CSV file, manipulate it and save it in a new path.

I want to delete columns 0, 1, 2 and 3 from the csv file instead of manipulating the data.

Can anyone help?

0

1 Answer 1

2

You can use code you posted, just change Save method, initialize col variable with 4 and you should be done.

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

Update:

To exclude 10th column, skip writing data in the 10th position.

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I just made this amend and achieved the desired outcome, just out of curiousity, how would I proceed if I wished to remove column 10 also?
You will have to add if cause inside inner loop to skip that column.
Would this be inside the if (col < _cols - 1) { stream.Write(delim); } clause?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.