【问题标题】:Append multiple attribute values inside csv在 csv 中附加多个属性值
【发布时间】:2015-11-18 16:45:15
【问题描述】:

我已将数据存储在以下 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 并通过/[\]]/ 寻找价值

【问题讨论】:

    标签: ruby json csv hash grep


    【解决方案1】:

    您应该尽量避免自己编写 CSV,Ruby 包含一个 CSV 类,它会为您完成所有转义。

      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
    

    【讨论】:

    • 我试图在没有宝石的情况下做到这一点。如果是默认库,应该没问题。
    • 那里有更快的 CSV gem,但这是一个标准的 Ruby 类。
    猜你喜欢
    • 2011-11-03
    • 2022-01-12
    • 2014-10-20
    • 1970-01-01
    • 2014-12-17
    • 2016-09-13
    • 1970-01-01
    • 2015-11-20
    • 2020-07-12
    相关资源
    最近更新 更多