English 中文(简体)
CSV到Yaml档案的书写
原标题:Write from CSV to yaml file?

如果有人能帮助我把这一结果推到一个半月的档案中,那么他会感到 Wonder?

截至目前,这只写上了最后一批回到了Myaml档案。

       require  csv 
       require  yaml 
          #fp = File.open("vatsim-row.txt")

CSV.foreach("vatsim-data.txt", :col_sep => : , :row_sep =>:auto, :quote_char => ":") do |row| 

if (row[3] == "PILOT") and (row[13]=="EIDW" or row[13]=="EICK" or row[13]=="EINN" or row[13]=="EIKN" or row[13]=="EIDL" or row[13]=="EICM" or row[13]=="EIKY" or row[13]=="EISG" or row[13]=="EIWF" or row[13]=="EIWT" or row[11]=="EIDW" or row[11]=="EICK" or row[11]=="EINN" or row[11]=="EIKN" or row[11]=="EIDL" or row[11]=="EICM" or row[11]=="EIKY" or row[11]=="EISG" or row[11]=="EIWF" or row[11]=="EIWT")

p row 

p row.count

File.open("pilots.yml", "w") {|f| f.write(row.to_yaml) }

elsif row[3] == "ATC" and (row[0].slice(0, 3) == "EGC" or row[0].slice(0, 3) == "EID" or row[0].slice(0, 3) == "EIC" or row[0].slice(0, 3) == "EIN" or row[0].slice(0, 3) == "EIK" or row[0].slice(0, 3) == "EIS" or row[0].slice(0, 3) == "EIW" or row[0].slice(0, 3) == "EIM")

              p row
              p row.count
            end

      end

yam yam yam

---
clients: 
 callsign: RYR87LN
    cid:        "123456"
    name:       Joe Blogs EIDW
    type:       PILOT
    lat:        "48.28681"
    long:       "-4.03478"
    altitude:   "30883"
    groundspeed:"438"
    aircraft:   B738
    p_cruise:   "300"
    dep:        LFRS
    p_alt: FL310
    arr: EIDW
    server: EUROPE-C2
    pro_rev: "100"
    rating: "1"
    squawk: "2200"
    facilitytype:
    vis_range: 
    p_flighttyp: "0"
    route: FPL-RYR87LN-IS-B738/M-ZSRWY/S-REG/EI-DAH COM/TCAS RVR/200 OPR/RYRVIRTUAL.COM DOF/110813- A/BLUE/WHITE/YELLOW /V/ TERPO UM616 KORER UN482 DEGEX UN490 BERAD UM142 INSUN UN34 EVRIN N34 BUNED
    atismsg:
    lt_atis:
    logon: "20110813151905"
    heading: "310"
    qnh_ig: "29.79"
    qnh_mb: "1008"

如果有人能够把我引向伟大的方向!

最佳回答

你们需要打开文件备份。 开启一份附有模式的文档:w, 将其长度限定为0, 有效来说,你重写了整条。

使用:

File.open("pilots.yml", "a") {|f| f.write(row.to_yaml) }

更有甚者,将文件开放,以便通过方案而不是反复开放和关闭。 之后,你可能希望打上档案:

File.open("pilots.yml", "w") do |f|
  CSV.foreach("vatsim-data.txt", :col_sep => : , :row_sep =>:auto, :quote_char => ":") do |row| 
    # ...

    f.write(row.to_yaml)

    # ...
  end
end
问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签