hi. i am creating a new csv file pro grammatically in C# where i am going to take values from a csv file that is already present with me.

consisder this csv file,

firstname,lastname,middle,company,email
xxxxxx,xxxxxxx,xxxxx,xxxxxxx,xxxxx
yyyyyy,yyyyyy,yyyyyy,yyyyyy,yyyyyy
zzzzzz,zzzzzz,zzzzzz,zzzzzz,zzzzzz

I need to read certain values from each row of this file and then do some coding for each value and create a whole new csv file with these new values.

like when i read the company column of row 1, then i need to code some function where i will need to specify where this company is located.

how can i read each value and and create a new csv with my functions for the new columns in the csv?

i tried csv parser but not much help.
Any suggestions?

Could you be more specific?
Do you mean to parse the header line for the word "compagny" and then get to the corresponding xxx...,yyy... and zzz... values?

>how can i read each value and and create a new csv with my functions for the new columns in the csv?

1. Use System.IO.File.ReadAllLines("file.csv") method to read all lines from the file.
2. String.Split() method to split each line and store result into new file.

Could you be more specific?
Do you mean to parse the header line for the word "compagny" and then get to the corresponding xxx...,yyy... and zzz... values?

i want to read specific fields in the input.csv file (like company) and use the value in that field to computing a function (a new field) in the output.csv

it is more like select company from table_name where row ID =1 and use this value to do some function and make an entry into the new csv file. the similar thing i want to do in a csv file. i hope i am clear.

i want to use the csv file as i use a database to get specific values and use those values to generate a new file.

Edited 6 Years Ago by scorpio222: n/a

hi. i am creating a new csv file pro grammatically in C# where i am going to take values from a csv file that is already present with me.

consisder this csv file,

firstname,lastname,middle,company,email
xxxxxx,xxxxxxx,xxxxx,xxxxxxx,xxxxx
yyyyyy,yyyyyy,yyyyyy,yyyyyy,yyyyyy
zzzzzz,zzzzzz,zzzzzz,zzzzzz,zzzzzz

I need to read certain values from each row of this file and then do some coding for each value and create a whole new csv file with these new values.

like when i read the company column of row 1, then i need to code some function where i will need to specify where this company is located.

how can i read each value and and create a new csv with my functions for the new columns in the csv?

i tried csv parser but not much help.
Any suggestions?

Add this class

public class Company {

       public string Firstname { get; set; }
       public string Lastname { get; set; }
       public string Middle { get; set; }
       public string Company { get; set; }
       public Email { get; set; }

public Company()
{

}

    public Company(string firstname, string lastname, string middle, string company, string email)
{
         Firstname = firstname;
         Lastname = lastname;
         Middle = middle;
         Company = company;
         Email = email;  
}

}

And then to read and treat everything as "columns".

System.IO.StreamReader sr = new StreamReader(csvpath);
string content = sr.ReadToEnd()
sr.Close();

string[] colsandrows = content.Split(',');

List<Company> companies = new List<Company>();

for( int i = 0; i < colsandrows.length; i+=5 ) // because you have 5 rows
{
        Company temp = new Company();
// you can do it this way, or simply pass every parameters in constructor
        temp.Firstname =          colsandrows[i] ;
        temp.Lastname =          colsandrows[i+1];
        temp.Middle =          colsandrows[i+2];
        temp.Company =          colsandrows[i+3];
        temp.Email =          colsandrows[i+4];   

       companies.Add(temp);
}

ANd there you go you can know access everything easily. Let me know if you have any other questions with LINQ or iterations (LINQ will provide better performance and this is really like a query that you can apply on collections)

Edited 6 Years Ago by PierlucSS: n/a

Add this class

public class Company {

       public string Firstname { get; set; }
       public string Lastname { get; set; }
       public string Middle { get; set; }
       public string Company { get; set; }
       public Email { get; set; }

public Company()
{

}

    public Company(string firstname, string lastname, string middle, string company, string email)
{
         Firstname = firstname;
         Lastname = lastname;
         Middle = middle;
         Company = company;
         Email = email;  
}

}

i am getting an error at get set values. so i added extern after public for each string. it was executing but the file i wanted to create was not getting created.

ERROR: Could not load type 'RTC_Node.Company' from assembly 'RTC_Node, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'get_Firstname' has no implementation (no RVA).
This error is at ---- Application.Run(new Form1()); in program.cs file

i have a situation where i have to read a certain field of a csv file and check the value of that field and then say YES or NO depending on that value. i would like to how i can extract one value of a csv colum(from header) and get the value in that row associated with that header, evaluate my function and return the appropriate value. the function thing i can compute but the problem is how do i get to read one particualr field of the csv file and then execute the fuction. I do not want to read every file of the csv header...only one specific field.

i have 50 columns and 800 rows.

Edited 6 Years Ago by scorpio222: n/a

Perhaps try this:

// Suppose this is a line read from your cvs file
            string lineReadFromcvs = "firstname,lastname,middle,company,email";
            string[] items = lineReadFromcvs.Split(new char[] {','});
            string MyCompany = items[3];
            // MyCompany would now contain the word "company"

Perhaps try this:

// Suppose this is a line read from your cvs file
            string lineReadFromcvs = "firstname,lastname,middle,company,email";
            string[] items = lineReadFromcvs.Split(new char[] {','});
            string MyCompany = items[3];
            // MyCompany would now contain the word "company"

i am trying something like this:

StreamReader sr = new StreamReader(inputFile); 
            string[] allData = sr.ReadLine();             
                foreach (string line in allData)
                {
                   
                    string[] lineItems = line.Split(',');
                    if (lineItems[1] == "t2") // here i am getting the whole line into an array and then i can check whatever line i want with the [] and return appropriate result.
                        sb.AppendLine("2," + paramPath1);
                    else
                        sb.AppendLine("a," + paramPath1);
                    

                }
                using (StreamWriter outfile =
                           new StreamWriter(outputFileName))
                {
                    outfile.Write(sb.ToString());
                    outfile.Close();

                }

But i am getting an error with this code at the sr.Readline();
Cannot implicitly convert type 'string' to 'string[]'
What modification do i need to do to get this work?

i am trying something like this:

StreamReader sr = new StreamReader(inputFile); 
            string[] allData = sr.ReadLine();             
                foreach (string line in allData)
                {
                   
                    string[] lineItems = line.Split(',');
                    if (lineItems[1] == "t2") // here i am getting the whole line into an array and then i can check whatever line i want with the [] and return appropriate result.
                        sb.AppendLine("2," + paramPath1);
                    else
                        sb.AppendLine("a," + paramPath1);
                    

                }
                using (StreamWriter outfile =
                           new StreamWriter(outputFileName))
                {
                    outfile.Write(sb.ToString());
                    outfile.Close();

                }

But i am getting an error with this code at the sr.Readline();
Cannot implicitly convert type 'string' to 'string[]'
What modification do i need to do to get this work?

Readline returns one line at a time. This will get the result you are looking for:

string[] allData = sr.ReadToEnd().Split('\n');

This will split your string into many string at each line feed

Readline returns one line at a time. This will get the result you are looking for:

string[] allData = sr.ReadToEnd().Split('\n');

This will split your string into many string at each line feed

Thanks for replying.

this is the error at if (lineItems[1] == "t2")

Index was outside the bounds of the array.

i am getting an error at get set values. so i added extern after public for each string. it was executing but the file i wanted to create was not getting created.

ERROR: Could not load type 'RTC_Node.Company' from assembly 'RTC_Node, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'get_Firstname' has no implementation (no RVA).
This error is at ---- Application.Run(new Form1()); in program.cs file

i have a situation where i have to read a certain field of a csv file and check the value of that field and then say YES or NO depending on that value. i would like to how i can extract one value of a csv colum(from header) and get the value in that row associated with that header, evaluate my function and return the appropriate value. the function thing i can compute but the problem is how do i get to read one particualr field of the csv file and then execute the fuction. I do not want to read every file of the csv header...only one specific field.

i have 50 columns and 800 rows.

This is probably caused because you are using VS2005. Simply use public variables or get/set private variables

i.e.

public class Company
{
         private string _firstname;

         public string Firstname { get { return _firstname; } set { _firstname = value; } }

}

Thanks for replying.

this is the error at if (lineItems[1] == "t2")

Index was outside the bounds of the array.

This might be caused by empty lines in your csv. You should add a condition to filter these kind of lines.

This might be caused by empty lines in your csv. You should add a condition to filter these kind of lines.

StreamReader sr = new StreamReader(inputFile); 
            string[] allData = sr.ReadToEnd().Split('\n');            
                foreach (string line in allData)
                {
                    string[] lineItems = new string[1000];
                    lineItems = line.Split(',');
                     sb.AppendLine(lineItems[1] + "," + lineItems[1] + ",YES,NO,YES,NO,0,FLOAT,5,NO,NO,NO,NO,NO,NO,0,NO,NO INTERFACE,NONE,ER,15:00," + lineItems[1]+",NULL,NULL,"+lineItems[1]+","+lineItems[1]+",NO,NULL,NULL");
                }
                using (StreamWriter outfile =
                           new StreamWriter(outputFileName))
                {
                    outfile.Write(sb.ToString());
                    outfile.Close();

                }
            }

This is the present code that i am using. This code is working when the input file has <10 rows and <20 columns.
But the real data that i am working on has 5000 rows and 25 columns.

I tried to change string[] lineItems = new string[1000]; to 10,000 but no use when i try to access listItem[10]. same error. It says that there is only one value, listItem[0] and it is not grabbing every other value.

How can i add a condition to filter empty lines or fields which are empty in the csv.

Edited 6 Years Ago by scorpio222: n/a

StreamReader sr = new StreamReader(inputFile); 
            string[] allData = sr.ReadToEnd().Split('\n');            
                foreach (string line in allData)
                {
                    string[] lineItems = new string[1000];
                    lineItems = line.Split(',');
                     sb.AppendLine(lineItems[1] + "," + lineItems[1] + ",YES,NO,YES,NO,0,FLOAT,5,NO,NO,NO,NO,NO,NO,0,NO,NO INTERFACE,NONE,ER,15:00," + lineItems[1]+",NULL,NULL,"+lineItems[1]+","+lineItems[1]+",NO,NULL,NULL");
                }
                using (StreamWriter outfile =
                           new StreamWriter(outputFileName))
                {
                    outfile.Write(sb.ToString());
                    outfile.Close();

                }
            }

This is the present code that i am using. This code is working when the input file has <10 rows and <20 columns.
But the real data that i am working on has 5000 rows and 25 columns.

I tried to change string[] lineItems = new string[1000]; to 10,000 but no use when i try to access listItem[10]. same error. It says that there is only one value, listItem[0] and it is not grabbing every other value.

How can i add a condition to filter empty lines or fields which are empty in the csv.

Simply do this:

if(!string.IsNullOrEmpty(line))
{
     //do stuff
}

You can also do this for the column.

My best advice would be to make a class that encapsulate all of your possible fields and override the ToString method that will return a csv line (a complete row)

public string override ToString()
{
//Concat all your fields together in the correct orders using the private variables.
return csv;
}

Then you'll be able to simply add a ToString() at the end of your object and it'll show the csv value.

Try this,

string[] allData =File.ReadAllLines(inputFile);             
foreach (string line in allData)
  {
   string[] lineItems = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
 
  if(lineItems.Length>3) // Where value 3 is the minimum count of items
   {
    if (lineItems[1] == "t2") // here i am getting the whole line into an array and then i can check whatever line i want with the [] and return appropriate result.
                        sb.AppendLine("2," + paramPath1);
                    else
                        sb.AppendLine("a," + paramPath1);
         }               
      }
using (StreamWriter outfile =new StreamWriter(outputFileName))
  {
    outfile.Write(sb.ToString());
    outfile.Close();
 }

Try this,

string[] allData =File.ReadAllLines(inputFile);             
foreach (string line in allData)
  {
   string[] lineItems = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
 
  if(lineItems.Length>3) // Where value 3 is the minimum count of items
   {
    if (lineItems[1] == "t2") // here i am getting the whole line into an array and then i can check whatever line i want with the [] and return appropriate result.
                        sb.AppendLine("2," + paramPath1);
                    else
                        sb.AppendLine("a," + paramPath1);
         }               
      }
using (StreamWriter outfile =new StreamWriter(outputFileName))
  {
    outfile.Write(sb.ToString());
    outfile.Close();
 }

Heh, StringSplitOption, didnt know that, thanks =)

Split and objects are really the fastest way to read in text files, but if you know that the file will be all comma delimited, try using MS Jet OLE DB driver

http://www.connectionstrings.com/textfile

I used this to read in Google's GTFS files from other transit agencies so I could quickly spit out an XML file. The benefit for you is that it is accessible as a standard DataTable.

Use this only if you can absolutely be certain about columns are pre defined.

Thanks you PierlucSS and adatapost for helping me out.
I have played with it a little with your help and here is the working code.

the .Length(case-sensitive) thing is very important.

string[] arr = System.IO.File.ReadAllLines(inputFile);
            for (int i = 1; i < arr.Length; i++)
            {
                // Current line
                string[] lineItems = new string[5000];
                lineItems = arr[i].Split(',');
                sb.AppendLine(lineItems[10] + "," + lineItems[10] + ","+ lineItems[2] + ",NULL,NULL");
                
                using (StreamWriter outfile =
                           new StreamWriter(outputFileName))
                {
                    outfile.Write(sb.ToString());
                    outfile.Close();

                }
            }
This question has already been answered. Start a new discussion instead.