ruby - Append multiple attribute values inside csv -


i have stored data in following json/xml. please find below link. looking store values of des_facet, org_facet, per_facet, geo_facet in csv in array. @ moment values stored in hash map stores these values in separate column.

hash = article.attributes.select {|k,v| !["author","images","guid","link"].include?(k) } hash_new = [] hash.values.map |v|     hash_new.push("\""+v.to_s+"\"") end hash_new.map(&:to_s).join(", ") 

sample json:

{     "articles": [{         "results": [{             "title": "ad blockers , nuisance @ heart of modern web",             "summary": "the adoption of ad-blocking technology rising steeply. see existential threat online content know it, others see new business niche.",             "source": "http://www.nytimes.com/2015/08/20/technology/personaltech/ad-blockers-and-the-nuisance-at-the-heart-of-the-modern-web.html",             "date": "2015-08-20t00:00:00-5:00",             "section": "technology",             "item_type": "article",             "updated_date": "2015-08-19t16:05:01-5:00",             "created_date": "2015-08-19t05:00:06-5:00",             "material_type_facet": "news",             "abstract": "the adoption of ad-blocking technology rising steeply. see existential threat online content know it, others see new business niche.",             "byline": "by farhad manjoo",             "kicker": "",             "des_facet": ["online advertising", "computers , internet", "data-mining , database marketing", "privacy", "advertising , marketing", "mobile applications"],             "org_facet": ["adblock plus"],             "per_facet": "",             "geo_facet": ""         }]     }] } 

i want respective csv same format. below get.

"ad blockers , nuisance @ heart of modern web", "the adoption of ad-blocking technology rising steeply. see existential threat online content know it, others see new business niche.", "http://www.nytimes.com/2015/08/20/technology/personaltech/ad-blockers-and-the-nuisance-at-the-heart-of-the-modern-web.html", "2015-08-20t00:00:00-5:00", "technology", "article", "2015-08-19t16:05:01-5:00", "2015-08-19t05:00:06-5:00", "news", "the adoption of ad-blocking technology rising steeply. see existential threat online content know it, others see new business niche.", "by farhad manjoo", "", "["online advertising", "computers , internet", "data-mining , database marketing", "privacy", "advertising , marketing", "mobile applications"]", "["adblock plus"]", "", "" 

i not sure how this. quite new ruby. have thought of using grep , out value /[\]]/

you should try avoid writing csv yourself, ruby has csv class included escaping you.

  unwanted_attributes  = ["author", "images", "guid", "link"]   sanitized_attributes = article.attributes.select { |attribute_name, _|      !unwanted_attributes.include?(attribute_name)    }    csv_string = csv.generate |csv|     csv << sanitized_attributes.values   end 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -