0

I asked similar question before but haven't solved it yet.

I have amazon review data set and would like to convert it into csv format in JAVA. The original data that I have look like as follows:

product/productId: B00032K32A

product/title: Cruiser Accessories 21330 Slim Rim, Chrome License Plate Frame

product/price: 4.99

review/userId: A2O41UFL8HAQWV

review/profileName: Nick Nefsik

review/helpfulness: 4/4

review/score: 5.0

review/time: 1239667200

review/summary: It's slim, alright!

review/text: Similar to another review, I also found that this frame is more of a overlay to a license plate (sits on top of the plate), as opposed to securing the plate underneath it, if that makes sense.It just covers the edges of my AZ plate, which is fine, but I sure wouldn't want it to be any smaller around its outside perimeter. I also ordered the chrome covers for the screws (Cruiser Accessories 82030 Screw Covers, Chrome) that I was already using, and, altogether, it looks great, and is exactly the look I was going for.

product/productId: B00032K32A

product/title: Cruiser Accessories 21330 Slim Rim, Chrome License Plate Frame

product/price: 4.99

review/userId: A3V7H58BH72AYT

review/profileName: Illustratedman

review/helpfulness: 6/7

review/score: 5.0

review/time: 1199145600

review/summary: Nice...

review/text: I first purchased these for my new 2008 Honda Accord EX-L to complement

I want the output to be

product id,title,price,userid,profileName,helpfulness,score,time,summary,text

B00032k32A,Cruser accessories..,4.99,A3v7...,illustratedam,6/7,5.0,1199145600,nice..,i first purchased these fr my new .......

B00032k302X,Cruser accessories..,3.95,A3v7...,illustedam,2/7,4.0,1193445600,nice..,i first purchased these for my new .......

I have a pretty big data of 5GB. So i want to write to file.

I am a newbee to java and tried several different ways but still haven't succeeded it yet. Are there anyone who has a good thought about converting the original data type into csv format?

Thanks

This is the code that i wrote

**public class ConvertToCsv {

public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader("data/Movies.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("data/movies.csv"));

    String line;
    while((line = br.readLine()) != null){
        String[] values = line.split("/",-1);
        bw.write(values[0]+","+values[1]+","+values[2]+","+values[3]+","+values[4]+","+values[5]+","+values[6]+","+values[7]+","+values[8]+","+values[9]+"\n");

    }
    br.close();
    bw.close();

}

}

But it does not have the desired effect.

Greg91
  • 75
  • 1
  • 2
  • 11
  • Is the input to the program the a text file with the format posted above, or is it some kind of Java data structure? – jjm Nov 29 '14 at 22:41
  • @dnault I think the main problem here isn't writing it to a CSV, it's parsing the weird input format. In any case lets wait till OP clears that up before marking a duplicate. – jjm Nov 29 '14 at 22:42
  • 1
    Like @jjm said, how are you getting the data from Amazon? Is it in a text file? Are you reading it in and storing it some way? We need more information than the structure of the Amazon output dump. Please provide some code of what you have tried – David.Jones Nov 29 '14 at 22:43
  • @jjm Okay, retracted suggestion to close as duplicate. – dnault Nov 29 '14 at 22:45
  • @jjm it is an input to the the program in a .txt file – Greg91 Nov 29 '14 at 22:46
  • You say you've tried several different ways but still haven't succeeded. Can you tell us what you've tried, and why it didn't work? – dnault Nov 29 '14 at 22:46
  • Ok, so the problem is dealing with that wretched input format, not outputting CSV (see http://stackoverflow.com/questions/14226830/java-csv-file-easy-read-write) for that bit. Let me think about the first part a bit... – jjm Nov 29 '14 at 22:51
  • If you've tried several ways, why not show us your best attempt? Otherwise how is anyone to guess what incorrect assumptions you may be making or what errors might be in your code? – Hovercraft Full Of Eels Nov 29 '14 at 22:53
  • 1
    Yeah, @HovercraftFullOfEels is right, I could write a method to do this for you, but you'll learn a lot more about Java if we start with something you wrote and work from there. – jjm Nov 29 '14 at 22:56
  • So I've posted what I tried – Greg91 Nov 29 '14 at 23:45
  • Well, the lines only appear to contain one "/" character, so I don't think that will work at all. Is the input formatted the way you posted it? If not, maybe put a sample on pastebin or gist? – jjm Nov 30 '14 at 00:02
  • @jjm The input is exactly the way I posted it. – Greg91 Nov 30 '14 at 01:24
  • Ok, so record fields are separated by blank lines. So to read a record field, you'll want to read lines and append them to a StringBuilder until you hit a null or empty line. After reading a record field, split the value with `field.split(":", 1)`. The first element of the split array will be the field name, and the second the value. At this point you can figure out whether you're done with the previous record and starting the next. In any case, you can use a map of names to indices to figure out which row index to put the field value in. See where that gets you. – jjm Nov 30 '14 at 01:51
  • Or if you don't want to learn anything and just need to get this done I'll post a solution in a gist. – jjm Nov 30 '14 at 01:55
  • @jjm Thanks for your help. I do want to learn but got this going on for a few days now, so it would be helpful if you could post the solution. Thanks. – Greg91 Nov 30 '14 at 02:36
  • @Greg91 that was slightly harder than I thought it'd be...gist is https://gist.github.com/jjmason/6d3003789ded68418803. You need opencsv on your classpath, which you can get here: opencsv.sourceforge.net. If you have any questions leave them on the gist. – jjm Nov 30 '14 at 04:15

0 Answers0