在csv

时间:2015-08-25 05:33:50

标签: ruby json csv hash grep

我已将数据存储在以下JSON / XML中。请在下面找到链接。我希望将des_facetorg_facetper_facetgeo_facet的值存储在我的CSV数组中。目前,存储在哈希映射中的值将这些值存储在单独的列中。

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

示例JSON:

{
    "articles": [{
        "results": [{
            "title": "Ad Blockers and the Nuisance at the Heart of the Modern Web",
            "summary": "The adoption of ad-blocking technology is rising steeply. Some see an existential threat to online content as we know it, but others see a 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 is rising steeply. Some see an existential threat to online content as we know it, but others see a new business niche.",
            "byline": "By FARHAD MANJOO",
            "kicker": "",
            "des_facet": ["Online Advertising", "Computers and the Internet", "Data-Mining and Database Marketing", "Privacy", "Advertising and Marketing", "Mobile Applications"],
            "org_facet": ["Adblock Plus"],
            "per_facet": "",
            "geo_facet": ""
        }]
    }]
}

我希望相应的CSV格式相同。目前以下是我得到的。

"Ad Blockers and the Nuisance at the Heart of the Modern Web", "The adoption of ad-blocking technology is rising steeply. Some see an existential threat to online content as we know it, but others see a 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 is rising steeply. Some see an existential threat to online content as we know it, but others see a new business niche.", "By FARHAD MANJOO", "", "["Online Advertising", "Computers and the Internet", "Data-Mining and Database Marketing", "Privacy", "Advertising and Marketing", "Mobile Applications"]", "["Adblock Plus"]", "", ""

我不知道该怎么做。我对Ruby很新。我想过可能会使用grep并注意/[\]]/

的值

0 个答案:

没有答案