CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    1

    problem in reading a csv file

    Hi alll,

    I have a problem in reading a csv file. My probelm is when i read a comma seperated text file into a csv file,

    i got exta columns as there are commas between text. for example my text file is like this..........

    /........................../

    philipsco.ltd,usa,james

    k.w.p. philipines,co.ltd, new jersey, Rosy

    Samsung co,ltd, japan, rekhamithal

    /....................../

    when i read this file, comma betweem philipines,co.ltd is divided into two columns which has to be in one column.Pls help me.......i need it urgently

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: problem in reading a csv file

    Using my Crystal Ball I can tell that you are doing something wrong.

    Seriously, go back ad re-read your post. Remember we have NO other kowledge about what you are doing. HOW do you expect any of us to guess???
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jan 2005
    Location
    City of Smiles!
    Posts
    125

    Re: problem in reading a csv file

    hi cpuwiz,

    i'm guessing that what krish meant was something like split the following string:


    k.w.p. philipines,co.ltd, new jersey, Rosy


    into this:


    k.w.p. philipines,co.ltd
    new jersey
    Rosy


    but sinces it's a CSV, it's splitting it into this:


    k.w.p. philipines
    co.ltd
    new jersey
    Rosy


    i've been wrong before, though....



    cheers!
    ...we get by with a little help from our friends...

  4. #4
    Join Date
    Sep 2006
    Posts
    95

    Cool Re: problem in reading a csv file

    Your CSV Format isn't correct. in CSV files, text is usually stuffed between " marks. This prevents comma's in there from messing up your seperations. And " is Escaped by a 2nd ". Usually this is how CSV files are built up.

    So your code has to check if a value starts with a ". If that's the case, it's a text, and should keep reading until it hits another ". Then check if there's another " after it, in which case, one is dropped and you're still in text reading mode, so you go to the next ".
    When you find one that doesn't have a " after it, your text is completed and you can expect a , to switch to the next field.

    There are many variants on this idea, using ; instead of , to seperate values, or . or tabs, etc. Sometimes the first row will contain the column names, sometimes it's raw data. And ofcourse, back in the days that CSV was invented, object oriented coding and such was practicly non-existant, so you had to rewrite the whole thing again from scratch if you had to do another kind of import engine. ( read: copy-paste-coding )
    On Error Kill(User)

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: problem in reading a csv file

    however, there is something wrong with the input CSV file. AFAIK, text columns must be surrounded by double-quotes so that there would no confusions with commas..


    "philipsco.ltd", "usa", "james"
    "k.w.p. philipines,co.ltd", "new jersey", "Rosy"
    "Samsung co,ltd", "japan", "rekhamithal"
    Busy

  6. #6
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: problem in reading a csv file

    Thread1 is correct. According to the CSV specification, if there are commas within text then it should be surrounded by double quotes.

    This code should correctly parse any line within a CSV file:
    Code:
    string[] Lines = Regex.Split(Line, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured