从 CSV 写入 yaml 文件?

发布于 2024-11-29 07:56:21 字数 1948 浏览 0 评论 0原文

想知道是否有人可以帮助我将其输出到 yaml 文件?

目前它只将返回的最后一个数组写入 yaml 文件。

       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

我在 yaml 文件中寻找的输出类型:

---
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"

如果有人能指出我的方向,那就太好了!

Wondering if anyone can help me with outputting this to a yaml file?

As at the moment it only writes the last array returned to the yaml file.

       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

The sort of output i'm looking for in the yaml file:

---
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"

If anyone can point me in the direction that would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-12-06 07:56:21

您需要打开文件进行追加。使用模式 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

You need to open the file for appending. Opening a file with mode w truncates it to 0 length, effectively you're overwriting it every iteration of the loop.

Use this:

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

Better yet, leave the file open for appending through out the program instead of repeatedly opening and closing it. Then you probably will want to truncate the file:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文