Solved

Remove the first (Empty) column from a CSV file using C#

Posted on 2008-09-29
6
2,221 Views
Last Modified: 2013-12-17
Hello Experts,

I am receiving a CSV file from a third party. The file aways comes with the first Column of cells with no data. This is due to a comment at  location A1 on the spreadsheet. Data starts at B2.

I would like to remove this column so that the data in the files starts at A2 instead of B2 using C#

Does anyone know how to do this??

Thanks!
 
0
Comment
Question by:Saxitalis
  • 3
  • 3
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22601587
try with something like this:


using(TextReader reader = new StreamReader("yourfile.csv"))

{

     using(TextWriter writer = new StreamWriter("yourfile.csv.tmp")) 

     {

            do {

                 string line = reader.ReadLine();

                 if (line != null)

                        writer.WriteLine(line.Substring(1));

           } while (line != null);

      }

}
 

File.Delete("yourfile.csv");

File.Move('yourfile.csv.tmp", "yourfile.csv");

Open in new window

0
 

Author Comment

by:Saxitalis
ID: 22605097
Hi Jamie,

I tried your code but I can't seem to get it to work...

I moved the line declaration above the begining of the do loop due to scope:

 using(TextWriter writer = new StreamWriter("yourfile.csv.tmp"))
 {

***
  string line = reader.ReadLine();
***
 do {
            if (line != null)
             writer.WriteLine(line.Substring(1));
           } while (line != null);
}

When I run this code, the temp file grows out of hand. It looks like. The blank column is removed but it looks like an entire row of cells is saved back into each cell and repeated many times. The temp file does not stop growing until I break the process.

Any ideas what may be going on here?

Thanks!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22606284
sorry, I am writing from my memory, it should be:

string line;
using(TextReader reader = new StreamReader("yourfile.csv"))
{
     using(TextWriter writer = new StreamWriter("yourfile.csv.tmp"))
     {
            do {
                 line = reader.ReadLine();
                 if (line != null)
                        writer.WriteLine(line.Substring(1));
           } while (line != null);
      }
}
 
File.Delete("yourfile.csv");
File.Move('yourfile.csv.tmp", "yourfile.csv");
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Saxitalis
ID: 22609967
Thanks Jaime -  ths is pretty close to what I am after. I want to delete the entire column even if there is text in one of the cells (sorry if I was not clear on that)

See attached Excel CSV example screenshots:
Sheet1 is how it looks after Vendor sends it to me
Sheet2 is how I want it to look
Sheet3 is how it looks after running your code.

I would like to remove the text "Note from Vendor here" as well as effectively moving all the data to the left one row. This would be the same as highlighting the entire A column (on Sheet1) and selecting delete from the context menu when in Excell spreadsheet view.

Any Ideas?

Thanks!


RemoveColumnFromCSV.doc
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22610089
Which is your csv separator?
Assuming it is semi-colon, you should use:

string line;
char separator = ';';
using(TextReader reader = new StreamReader("yourfile.csv"))
{
     using(TextWriter writer = new StreamWriter("yourfile.csv.tmp"))
     {
            do {
                 line = reader.ReadLine();
                 if (line != null)
                        writer.WriteLine(line.Substring(line.IndexOf(separator)+1));
           } while (line != null);
      }
}
 
File.Delete("yourfile.csv");
File.Move('yourfile.csv.tmp", "yourfile.csv");
0
 

Author Closing Comment

by:Saxitalis
ID: 31501402
Thanks Jaime!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net/WinForms: adjust Screen Resolution 6 41
Tool Box 2 34
Error on Add method 1 38
C# Single Form 8 27
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now