Split CSV File

Hi

I have the data as following in CSV File

vikram,vijay,hai,"nice,100" boy, "good,200"

now how can i split this data , can you please suggest.
praveen1981Asked:
Who is Participating?
 
CimperialiCommented:
try this:
       const string fileContet =  "vikram,vijay,hai,\"nice,100\" boy, \"good,200\""; // <---replace this with your file content... 
        const string pattern = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
        private void button1_Click(object sender, EventArgs e)
        {
           string[] words = System.Text.RegularExpressions.Regex.Split(fileContet, pattern,System.Text.RegularExpressions.RegexOptions.ExplicitCapture    );
            foreach (string  s in words )
            {
                System.Diagnostics.Debug.WriteLine(s);    
            }       
        }

Open in new window

0
 
Pratima PharandeCommented:
StreamReader sr = new StreamReader("file.csv");
string inputLine = "";

String[] values = null;
while ((inputLine = input.ReadLine()) != null)
{
values = inputLine.Split(";");
}
input.Close();


 

 Loops over all the lines in the text file, and each time you have an string array with all the values for that line.
0
 
Sam Simon NasserIT Support ProfessionalCommented:
That would be very easy ...
in excel (2007/2010), from the ribbon, go to Data | From Text
1select your file
 2on the first windows click next
3NOW HERE IS THE MAGIC TRICK: click on comma
 4and that's it ... next next next ...

Hope this clarifies what you want.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
praveen1981Author Commented:
Hi

I want to do this in c#.net code

can u please suggest.
0
 
Pratima PharandeCommented:
have you tried this , this is C#.net code

StreamReader sr = new StreamReader("file.csv");
string inputLine = "";

String[] values = null;
while ((inputLine = input.ReadLine()) != null)
{
values = inputLine.Split(";");
}
input.Close();
0
 
praveen1981Author Commented:
Hi

I have tried the above which is not working.
0
 
nepaluzCommented:
This is an improperly formed comma separated file.
0
 
käµfm³d 👽Commented:
0
 
praveen1981Author Commented:
Excellent, it solved the problem,
Many Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.