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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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.
praveen1981Author Commented:
Hi

I want to do this in c#.net code

can u please suggest.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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();
praveen1981Author Commented:
Hi

I have tried the above which is not working.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nepaluzCommented:
This is an improperly formed comma separated file.
käµfm³d 👽Commented:
praveen1981Author Commented:
Excellent, it solved the problem,
Many Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.