Results 1 to 9 of 9
  1. #1
    dekha is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Post Reading CSV file with miltiline column

    hi,

    How to read csv file with multi line in one field.

    for example...
    -------------------------------------------
    ProductID |ProductName |code
    -------------------------------------------
    1 |abc |aaa
    -------------------------------------------
    2 |jhg |kjhu
    |lofju |hyggt
    |mnjh |
    --------------------------------------------
    3 |kjiuy |hjhut
    --------------------------------------------

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How to read csv file with multi line in one field

    * Decide on a class (or classes) instances of which will represent the data

    In your case it might be a Product class which holds an id and an list of name/code pairs. It might have a method to add a name/code pair to the list

    * Read the file line at a time, being careful to detect and respond appropriately to empty fields. If your data is CSV (not a completely uniform standard) then you might want to google and use someone else's implementation of all the messy details. In the past I have used a library by osertmiller.

    Validate your data to taste. But, in particular, you are interested in the whether the first field is empty.

    * If the first field is empty add the name/code pair to the Product you are currently bulding up.

    * If the first field is not empty, save the current Product to whereever you want it. And use the id name and code to start building a new Product.

  3. #3
    dekha is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Exclamation

    Thanks for your reply, I am new to java. could you post some sample java code to read the multiline column in CSV file?

    Thank you!

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    CSV means Comma-Separated Values. I don't see any commas in your file. I might assume that | characters are meant to be field separators, but the file still doesn't make any sense. Are there supposed to be three products there, with ProductID 1, 2 and 3? If so, what is the ProductName for ProductID 2? What is its code? Does ProductID 2 have three different ProductNames and codes?

    -Gary-

  5. #5
    dekha is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    In the attached CSV file, if u look at the row for productid-2 the productname is "jhg
    xxxxx
    yyyyy"

    that means while entering data in csv somebody pressed the enter key after each word. I need the product name as "jhg xxxxx yyyyy" in single line.

    please check the attachment.
    Attached Files Attached Files

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I don't write other people's code for them. There's nothing personal in that.

    There are a couple of reasons why I don't.

    (1) I don't think it helps anyone's understanding to bypass the opportunity to develop the skills and attitudes that writing code demands: thinking about problems, playing with problems, expressing problems clearly. Sometimes people say it's "problem solving": if only! Sometimes we solve. Sometimes we just think, play and express, and hope that perhaps tomorrow we solve.

    (2) It isn't fun. This reason takes much less to express, but it carries just as much weight for me.

    I think you'll find that others here are disinclined to hand out code as well: each for their own reasons.

    ------------------------------------

    You might also find that there are many, many here who are willing to help. But that starts with you. Find a problem that you feel you might be able to tackle (because if anybody solves the problem you pose, it will be you). Post your code. If it can't compile, ask about that. If it runs but doesn't give the answer, ask about that.

    ------------------------------------

    Forgive me if you've worked already on reading the CSV file and the "problem" is mostly one of language or hesitancy or whatever. But the solution remains the same: post what you've come up with (whatever that may be) and ask a specific question about that. Or ask a specific question about what I offered if it's unclear.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Why didn't you put this in your post in the first place? It's completely different from what you posted.
    Java Code:
    ProductID,ProductName,code
    1,abc,aaa
    2,"jhg
    xxxxx
    yyyyy","jjj
    xzxzxz"
    3,loi,lll
    This at least makes sense and is a proper CSV file. The line breaks are not a great difficulty, because those strings are properly quoted.

    Now post what you've written so far. Do you know how to set up your class? Do you know how to open the file and read from it? We're not going to write a whole program for you, so we need to see where you are getting stuck. Remember to use CODE tags when you post.

    -Gary-

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I assumed the | were field separators.

    In the file you posted the field values have newline characters in them. This doesn't display properly in (my version of) Excel, but it's valid CSV. I would still recommend the Ostermiller library to take care of actually reading the data. Then you can replace the new lines with spaces.

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2 View Post
    I assumed the | were field separators.

    In the file you posted the field values have newline characters in them. This doesn't display properly in (my version of) Excel, but it's valid CSV. I would still recommend the Ostermiller library to take care of actually reading the data. Then you can replace the new lines with spaces.
    What I posted is the contents of dekha's Book1.zip. I don't know why he didn't post that in the first place, as it makes more sense than the thing he posted originally.

    I'm pretty sure this is a homework assignment, so I doubt he's allowed to use external libraries. I believe I'd read the file a character at a time and use a state machine approach.

    Finite-state machine - Wikipedia, the free encyclopedia

    -Gary-

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  2. Add new column to csv file
    By Tota in forum New To Java
    Replies: 0
    Last Post: 08-31-2010, 03:32 PM
  3. Replies: 1
    Last Post: 07-06-2010, 04:50 PM
  4. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  5. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Posting Permissions

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