首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在csv中追加多个属性值

在csv中追加多个属性值
EN

Stack Overflow用户
提问于 2015-08-25 05:33:49
回答 1查看 28关注 0票数 0

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
    "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。下面是我得到的。

代码语言:javascript
复制
"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,并在/[\]]/中寻找价值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-25 05:48:54

您应该尽量避免自己编写CSV,Ruby有一个CSV类,它为您提供了所有转义功能。

代码语言:javascript
复制
  unwanted_attributes  = ["author", "images", "guid", "link"]
  sanitized_attributes = article.attributes.select { |attribute_name, _| 
    !unwanted_attributes.include?(attribute_name) 
  }

  csv_string = CSV.generate do |csv|
    csv << sanitized_attributes.values
  end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32196217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档