0

I have a CSV ordered based on the value of the "url" column. I would like to select the first three rows for each "url" value. If the rows, for the url value, are less than 3 (2 or 1), I would like to select them anyway.

The starting CSV is the following:

 url;review;priority;length
 zonk-di-brigada-igor-torino;"Ora sono tornati i vecchi gestori.";0;15
 zonk-di-brigada-igor-torino;"Buona qualità dei prodotti.";0;13
 zonk-di-brigada-igor-torino;"Ultimamente però l'apericena è scaduto.";0;13
 zonk-di-brigada-igor-torino;"I coktail sono buoni.";0;11
 zonk-di-brigada-igor-torino;"Per non parlare dei cocktail.";0;10
 zonk-di-brigada-igor-torino;"Locale molto bello.";0;7
 zichella-torino;"Che pasticceria";1;19
 zichella-torino;"Bar pasticceria di classe.";1;18

I want to obtain:

 url;review
 zonk-di-brigada-igor-torino;"Ora sono tornati i vecchi gestori."
 zonk-di-brigada-igor-torino;"Buona qualità dei prodotti."
 zonk-di-brigada-igor-torino;"Ultimamente però l'apericena è scaduto."
 zichella-torino;"Che pasticceria"
 zichella-torino;"Bar pasticceria di classe."

I started with this code but selects only one row.

 ArrayList<String> urls = new ArrayList<String>();
 String url, text;
 for (CSVRecord csvRecord : csvParserMatrix) 
 {
   url = csvRecord.get("url");
   text = csvRecord.get("review");
   if(!urls.contains(url))
   {
        urls.add(url);
        bw.write(url+";"+'"'+text+'"'+"\n");
   }
 }
3
  • What CSV-processing library are you using? Add a Tag or a mention. Commented Jun 18, 2019 at 16:13
  • apache commons csv Commented Jun 18, 2019 at 16:48
  • Post that information as an edit to your Question rather than as a Comment. Your audience should not have to trawl through comments to make sense of your Question. Also, notice how I added a Tag as well. Commented Jun 18, 2019 at 16:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.