一起打印哈希 CSV 输出 RUBY
Printing Hashes together CSV output RUBY
我正在学习 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')
正如您在代码的第一部分中看到的那样,我正在请求(害虫)作为哈希的信息包含什么(这只是几行):
{"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 格式打印出来 headers
{"eppocode"=>"ANIDMA", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"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"
而我想要的是避免出现一堆具有相同信息的字段。
我希望你明白我的问题
谢谢!
我认为,为了用最少的更改修复您的代码,您需要将 CSV 的发布更改为 resultTax
循环之外,所以像下面这样的东西可能有用吗?:
planta = h.values
resultTax.each do |tax|
puts "#{tax["eppocode"]} #{tax["prefname"]}"
planta << tax["eppocode"] +", "+ tax["prefname"]
end
csv << planta
(请原谅任何打字错误,发自我的phone)
我正在学习 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')
正如您在代码的第一部分中看到的那样,我正在请求(害虫)作为哈希的信息包含什么(这只是几行):
{"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 格式打印出来 headers
{"eppocode"=>"ANIDMA", "idclass"=>9, "labelclass"=>"Host", "fullname"=>"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"
而我想要的是避免出现一堆具有相同信息的字段。 我希望你明白我的问题 谢谢!
我认为,为了用最少的更改修复您的代码,您需要将 CSV 的发布更改为 resultTax
循环之外,所以像下面这样的东西可能有用吗?:
planta = h.values
resultTax.each do |tax|
puts "#{tax["eppocode"]} #{tax["prefname"]}"
planta << tax["eppocode"] +", "+ tax["prefname"]
end
csv << planta
(请原谅任何打字错误,发自我的phone)