Asynchronous
Asynchronous

Reputation: 3977

How to replace quotes in code for Reading Text File to CSV without causing character truncation?

I have tried and search but cannot understand or fix this problem.

Long story short. I need to read a Text file and Create as CSV. Things are looking good except for the following areas:

The first Column Name in the file gets truncated or shaved off the first Character: Example: If the column name is: Musician then it turns out like: usician, and cutting off the first character of all the data in that column.

This happens when I do this:

foreach (string line in s.Replace("\"", "").Split('\n'))

And if I write the code like this:

foreach (string line in s.Replace("\r", "").Split('\n'))

Then the column looks like this: Musician", so all the data including the header in the first column ends with a quote.

If I change the index position all gets in okay, except several data at the end of the file is shaved off.

Here is the full code: Please note that I am using an additional Replace Method to remove additional Commas, so that I can use the file in SSIS. Which is the main reason I need this.

static void TxtToCSV(string s, TextWriter writer)
{
foreach (string line in s.Replace("\r", "").Replace(", ", "").Split('\n'))
{
    for (int i = 0; i < line.Length; i++)
    {
        if (i > 0)
            writer.Write(line[i]);
    }
    writer.WriteLine();
}
}

static void Main(string[] args)
{
TextReader reader = new StreamReader(@"C:\folder\sample\test.txt");
string a = reader.ReadToEnd();
reader.Close();

FileStream aFile = new FileStream(@"C:\folder\sample\test.csv", FileMode.Create);
TxtToCSV(a, new StreamWriter(aFile));
aFile.Close();
}

Thanks for looking.

Upvotes: 0

Views: 1133

Answers (3)

ZimbiX
ZimbiX

Reputation: 495

It's a little hard to know what you're trying to do exactly without a sample input file, but in answer to your problem, it looks like the purpose of iterating over the characters in each line is to output all but the first character. I imagine this works to remove the first quote around "Musician", but then when you changed it to remove both quotes before it does that, the first character of the data is omitted instead.

Upvotes: 0

Marcus Ha&#223;mann
Marcus Ha&#223;mann

Reputation: 118

You did not write the first letter of the line. I also merged your replacing of carriage return and line feed into one replacing of Environment.NewLine. You should also flush the writer before closing or set the writer.Autoflush property to true.

Here is my optimized code:

    static void TxtToCSV(string s, TextWriter writer)
    {
        foreach (var line in s.Replace(", ", "").Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
        {
            foreach (var t in line)
            {
                writer.Write(t);
            }
            writer.WriteLine();
        }
        writer.Flush();
    }

Upvotes: 1

Alois Kraus
Alois Kraus

Reputation: 13535

You seem to try to handle quotes correctly. Your posted code does not show this. As you have figured out it can lead to off by one errors easily.

I would recommend to use a CSV reader library such as this one. It does work and it is most likely faster and certainly much more flexible as your current code. If you do not like an external library dependency you can simply use the source code and embed it to your project. It is quite small but very good code.

Upvotes: 1

Related Questions