Defining Comment Character in CSV File

Ask general questions here.
costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Defining Comment Character in CSV File

Post by costamesakid » Thu Mar 11, 2010 4:57 pm

This is more of a C# question but Im going to post it on the Ranorex forum as well.

I have a test that reads in records from a CSV file to populate a form. The CSV file is exceedingly long so I was hoping to use comments to section it out. The problem is that there is no comment character standard for a CSV file, but I did hear that you can define your own comment character in the C# file that reads the CSV file. For instance "skip rows that start with '#'".

The C# file that I am actually using to read the CSV file is CSVConnector.cs which is packaged with Ranorex in the Data Driven sample. Is it possible to define a comment character in this file, and if so can I possibly get an example of the code?

Possibly this is something Ranorex can incorporate in there CSVConnector.cs file in future releases? Thanks

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Defining Comment Character in CSV File

Post by Ciege » Thu Mar 11, 2010 7:23 pm

I've not looked at the CSVConnector.cs file but you could do your own code very easily...
Somethign like this.

Code: Select all

string strFile1 = "myfile.txt";
using (StreamReader li = new StreamReader(strFile1))
{
 while (true)
  {
  if (li.EndOfStream)
  break;
  string liTxt = li.ReadLine();
  if (liTxt.StartsWith("#")
   {
      //skip line
   }
   else
   {
     //continue doing what you need
   }
  }
}
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Re: Defining Comment Character in CSV File

Post by costamesakid » Wed Mar 31, 2010 8:45 am

Thanks Ciege.

I tried a few variations of this code but could not get it to work. Its a minor syntax error for sure so I pawned it off to one of our developers.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Defining Comment Character in CSV File

Post by Ciege » Wed Mar 31, 2010 3:55 pm

What error (if any) are you getting? Is it a compile error or a runtime error? Please add some more information so I can help out.
It wasn’t a direct copy/paste from my code base since I had to remove a bit of information before I was able to post the code so I may have fat-fingered something.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Re: Defining Comment Character in CSV File

Post by costamesakid » Wed Apr 28, 2010 3:53 am

Well... I have been assigned to re-investigate this issue and non of my attempts to implement Ciege's code are working. For starters, I have no idea where to implement Ciege's code within the current CSV Connector file. I also cannot find the C# syntax to skip a line that begins with a certain character. I have pasted the Ranorex CSV Connector code below. If I could get a little more direction with this issue it would be greatly appreciated. It just seems like this should be a much easier then I am making it out to be. I really would have figured if the '.Split' method defines the column character then there certainly should be a method to define the comment character?

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;


/// <summary>
/// Represents a data connector to a CSV-file
/// </summary>
class CSVConnector
{
private string fileName = null;
private DataTable dt = null;

/// <summary>
/// Initializes a new instance of the <see cref="CSVConnector"/> class.
/// </summary>
/// <param name="fileName">The path to the CSV file <see cref="CSVConnector"/></param>
public CSVConnector(String fileName)
{
try
{
if (fileName == null)
return;

this.fileName = fileName;
dt = new DataTable();
ParseCSVData();
}
catch (Exception e)
{
Console.WriteLine("{0} Open Connector Error: ", e.ToString());
}
}

/// <summary>
/// Gets the Data Collection Rows
/// </summary>
public DataRowCollection Rows
{
get { return dt.Rows; }
}

/// <summary>
/// Gets the Data Columns
/// </summary>
public DataColumnCollection Header
{
get { return dt.Columns; }
}

private void ParseCSVData()
{
try
{
String[] csvData = System.IO.File.ReadAllLines(fileName);

if (csvData.Length == 0)
return;

String[] headings = csvData[0].Split(';');

foreach (string header in headings)
{
dt.Columns.Add(header, typeof(string));
}

for (int j = 1; j < csvData.Length; j++)
{
DataRow row = dt.NewRow();

for (int i = 0; i < headings.Length; i++)
{
row = csvData[j].Split(';');
}

//if (csvData[j].StartsWith['#'])

dt.Rows.Add(row);
}
}
catch (Exception e)
{
Console.WriteLine("{0} Parse CSV Data Error: ", e.ToString());
}
}
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Defining Comment Character in CSV File

Post by Support Team » Thu Apr 29, 2010 4:07 pm

Hi!

IMHO, it would be more usefull to place the if statement inside the for loop.
for (int j = 1; j < csvData.Length; j++)
{
    DataRow row = dt.NewRow();

    //If row does not start with "#" go into the if statement and execute code inside.
    if(!csvData[j].StartsWith("#")) 
    {
        for (int i = 0; i < headings.Length; i++)
        {
            row = csvData[j].Split(';');
        }
        dt.Rows.Add(row);
    }
}


Regards,
Peter
Ranorex Support Team
.
Image

costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Re: Defining Comment Character in CSV File

Post by costamesakid » Fri Apr 30, 2010 5:13 pm

Thanks Peter

This works perfect for what I need the code to do.