hey can anyone help me to how to read a csv file

csharp Malawi
  • 9 years ago

    hi all,

    i have a problem in reading a csv file. when i read a csv file and write into another file, all the text is coming into only 1 column. the code i have written is:

    StreamReader sr = new StreamReader("input.csv");

    StreamWriter sw = new StreamWriter("output.csv");

    string sdata;while ((sdata = sr.ReadLine()) != null)

    {

    string[] str_arr = sdata.Split(',');

    foreach (string sfinal in str_arr)

    {

    sw.WriteLine(sfinal);

    }

    }

    sr.Close();

    sw.Close ();

    so, pls help me how to seperate into different fields.

  • 9 years ago

    The input CSV file is already split into fields. So you could simply copy the whole file and you have the fields left as fields.. :)

    What format do you want your output file to be in?

    Using Split() will get you the words, without the commas present.

    If you want the fields seperated by tab characters, all you need do is read a source line, replace the commas by tabs in the whole string at once, and then write it out.

    If you want spaces between, then you need to add the spaces on your WriteLine row of code.

     

     

  • 9 years ago

    I want the output file also in csv file. When i replace comma with "\t" , am getting some small box and my entire text has come into only 1 column. I want them into different columns. so can u pls tel what to do further

  • 9 years ago

    I dont understand.

    The input file is a CSV file and the output file is supposed to be a CSV file.

    Why not just copy the file? What else do you need to do with it?

  • 9 years ago

     

    Actually i have to seperate the csv into different fields. The input file is not divided into different columns, like the whole text is in one column with commas seperated. so i have to seperate commas between them and place them into different columns. I have replaced comma with tab though am not getting
  • 9 years ago

    Hi Priya,

    Let me understand ur problem. You have Data,for example - Name1,Rollno,marks1,mark2,total,Name2,Rollno,marks1,mark2,total,Name3,Rollno,marks1,mark2,total,Name4,Rollno,marks1,mark2,total,Name5, Rollno,marks1,mark2,total,...etc.

     Now u want it in Field seperated right.

    If this is your problem

    then all u need to do is to Know the field repeatation. and Do not write the Comma while writing in the second file when the record ends. like this

    Name1,Rollno,marks1,mark2,total

    Name2,Rollno,marks1,mark2,total

    Note that there is no Coma after total but a \n

    Such a file if u open in Excell it will show you the Data in Rows and columns.

    Relacing Comma with the Tabs will not help. U need the \n for the Record seperator.

    If needed I can provide the code snippet if that is the problem .

    SC.

  • 9 years ago

     

    hii, am totally confused with what u say. If u dont mind can  u put that it into code
  • 9 years ago

    Just put the Sample data and expected output if u need help.

  • 9 years ago

    hii, Here is my data. I have like this in so many rows and all these has there in one column.

     ID,Compant, Fax,Phone,Email

    2841,NULL,(510)528-5483,(562)432-3386,NULL

    2842,Sage Electric,(805)922-3195,(805)349-9853,sage-electric@ix.netcom.com

    2843,Fastdel Transport,(951)538-7500,(951)674-1488,NULL
    2844,Employment Hotline,(520)795-1907,(520)795-1987,NULL

    2845,Kevin Ngo Trucking,(510)538-1153,(562)432-3386,NULL
    2846,Driggs Corporation,(301)499-1900,(301)350-2592,NULL

     So, i have to seperate them into different columns. As earlier i said i used  '\t' to keep them into seperate columns. if u could do any help , i'll be so happy.

     Thanks in advance

  • 9 years ago

    But they are already in seperate columns.

    the commas tell you where the columns start.

    Surely the problem here is the word 'column'

    This only has a meaning when you display the data.

    For instance, if you open this file in Excel, it will put the data into real columns for you, where the commas appear.

    If you open the file in notepad, it all looks like one line.

     

    You have already had people suggest you change the commas into \t  (tab) characters.

    If you do this, the file will start to look like

    2841        NULL      (510)... etc

     

    So, the big question:

    What application do you want to see this data in, shown in columns?

    Or do you want to print it?

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous