【问题标题】:Printing Hashes together CSV output RUBY一起打印散列 CSV 输出 RUBY
【发布时间】:2022-01-10 18:00:08
【问题描述】:

我正在学习 Ruby,我需要 CSV 文件方面的帮助。 我没有找到将输出打印到 CSV 文件的正确方法,但我得到了递归性,我不希望这样。 就是代码:

require "httparty"
require "csv"

class ConnexioEPPO
    include HTTParty
    base_uri 'https://data.eppo.int/api/rest/1.0'
    @@authtoken = "a9505d2ab257987580641d1a56de1f6c"

    def pests(eppocode)
        request = self.class.get("/taxon/#{eppocode}/pests?authtoken=#{@@authtoken}")
        result = request.parsed_response

        CSV.open("data.csv", "w", headers: result["Host"].first.keys) do |csv|
            result["Host"].each do |h|
                requestTax = self.class.get("/taxon/#{h["eppocode"]}/taxonomy?authtoken=#{@@authtoken}")
                resultTax = requestTax.parsed_response
                puts h["eppocode"]

                resultTax.each do |tax|
                    puts "#{tax["eppocode"]} #{tax["prefname"]}"
                    
                    planta = h.values << tax["eppocode"] +", "+ tax["prefname"]
                    csv << planta 
                end
            end
        end
    end
end

connexioEPPO = ConnexioEPPO.new
puts connexioEPPO.pests('1ULMG')

正如您在代码的第一部分中看到的那样,我请求(害虫)作为 Hash 包含的信息(这只是几行):

{"eppocode"=>"ANIDMA", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Anisandrus maiche"}
{"eppocode"=>"ANOLGL", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Anoplophora glabripennis"}
{"eppocode"=>"APRIGE", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"Apriona germari"}
{"eppocode"=>"PHYPUL", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"'Candidatus Phytoplasma ulmi'"} 

然后我再次请求(分类)信息,我正在做的是获取新请求中字段 eppocode 等于 eppocode 的信息(这个想法只是获取以前的分类eppocode请求的字段)

我们的想法是在 CSV 中打印出第一个标题 {"eppocode"=&gt;"ANIDMA", "idclass"=&gt;9, "labelclass"=&gt;"Host", "fullname"=&gt;"Anisandrus maiche"} 并添加对应字段的分类

希望的输出(以一个 eppocode 为例):

ANIDMA,9,Host,Anisandrus maiche,"1ANIMK, Animalia","1ARTHP, Arthropoda","1INSEC, Insecta","1COLEO, Coleoptera","1CURCF, Curculionidae","1SCOLS, Scolytinae","1ANIDG, Anisandrus","ANIDMA, Anisandrus maiche"

实际输出(以一个eppocode为例):

ANIDMA,9,Host,Anisandrus maiche,"1ANIMK, Animalia"
ANIDMA,9,Host,Anisandrus maiche,"1ARTHP, Arthropoda"
ANIDMA,9,Host,Anisandrus maiche,"1HEXAQ, Hexapoda"
ANIDMA,9,Host,Anisandrus maiche,"1INSEC, Insecta"
ANIDMA,9,Host,Anisandrus maiche,"1COLEO, Coleoptera"
ANIDMA,9,Host,Anisandrus maiche,"1CURCF, Curculionidae"
ANIDMA,9,Host,Anisandrus maiche,"1SCOLS, Scolytinae"
ANIDMA,9,Host,Anisandrus maiche,"1ANIDG, Anisandrus"
ANIDMA,9,Host,Anisandrus maiche,"ANIDMA, Anisandrus maiche"

我想要的是避免使用相同信息的一堆字段。 我希望你能理解我的问题 谢谢!

【问题讨论】:

    标签: ruby csv file hash


    【解决方案1】:

    我相信,为了通过最小的更改来修复您的代码,您需要将发布到 CSV 的内容更改为在您的 resultTax 循环之外,所以类似以下的方法可能有效?:

    planta = h.values
    resultTax.each do |tax|
      puts "#{tax["eppocode"]} #{tax["prefname"]}"
      planta << tax["eppocode"] +", "+ tax["prefname"]
    end
    csv << planta
    

    (请原谅任何错别字,从我的手机发帖)

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2019-01-13
    • 2018-08-08
    • 2015-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    相关资源
    最近更新 更多