首页 > 解决方案 > 当其中有数据时,为什么这个数组元素会返回 nil?

问题描述

我正在解析 CSV 中的行,这是每个行对象的外观示例:

 pry(Program)> row
=> #<CSV::Row "Broadcast Name":"2020 FC Cincinnati vs Toronto FC | MLS" "Description":"Major League Soccer is a men's professional soccer league sanctioned by the United States Soccer Federation which represents the sport's highest level in the United States. The league comprises 26 teams—23 in the U.S. and 3 in Canada and constitutes one of the major professional sports leagues in both countries." "Category":"Sports,Soccer" "Sizzle Reel":"https://youtu.be/fM5aHIVBTIc" "Thumbnail Image":"Screen Shot 2020-01-14 at 5.27.31 PM.png (https://dl.airtable.com/.attachments/7d9273fef3fc1cf1a44c4f2c4db395e7/05f3cd4a/ScreenShot2020-01-14at5.27.31PM.png)" "Header Image":"Screen Shot 2020-01-14 at 5.27.31 PM.png (https://dl.airtable.com/.attachments/7d9273fef3fc1cf1a44c4f2c4db395e7/05f3cd4a/ScreenShot2020-01-14at5.27.31PM.png)" "Source":"Flosports" "Date":"3/21/2020" "Marketing":"https://www.mlssoccer.com/" "Total Available Impressions":"10.0MM" "Programming Type":"Live - VOD" "Demo":"P18-49" "Recommended":"YES" "Featured":"YES" nil:"10000000">

但是,每当我尝试通过它返回的键名访问第一个元素时nil

> row["Broadcast Name"]
=> nil

当我尝试通过数组中的索引访问它时,它返回正确的结果:

 row[0]
=> "2020 FC Cincinnati vs Toronto FC | MLS"

当我通过键名访问任何其他元素时,它会起作用:

> row["Description"]
=> "Major League Soccer is a men's professional soccer league sanctioned by the United States Soccer Federation which represents the sport's highest level in the United States. The league comprises 26 teams—23 in the U.S. and 3 in Canada and constitutes one of the major professional sports leagues in both countries."
[18] pry(Program)> row["Category"]
=> "Sports,Soccer"
[19] pry(Program)> row["Sizzle Reel"]
=> "https://youtu.be/fM5aHIVBTIc"

当我将其转换为哈希时,它似乎很好:

> row.to_h
=> {"Broadcast Name"=>"2020 FC Cincinnati vs Toronto FC | MLS",
 "Description"=>
  "Major League Soccer is a men's professional soccer league sanctioned by the United States Soccer Federation which represents the sport's highest level in the United States. The league comprises 26 teams—23 in the U.S. and 3 in Canada and constitutes one of the major professional sports leagues in both countries.",
 "Category"=>"Sports,Soccer",
 "Sizzle Reel"=>"https://youtu.be/fM5aHIVBTIc",
 "Thumbnail Image"=>"Screen Shot 2020-01-14 at 5.27.31 PM.png (https://dl.airtable.com/.attachments/7d9273fef3fc1cf1a44c4f2c4db395e7/05f3cd4a/ScreenShot2020-01-14at5.27.31PM.png)",
 "Header Image"=>"Screen Shot 2020-01-14 at 5.27.31 PM.png (https://dl.airtable.com/.attachments/7d9273fef3fc1cf1a44c4f2c4db395e7/05f3cd4a/ScreenShot2020-01-14at5.27.31PM.png)",
 "Source"=>"Flosports",
 "Date"=>"3/21/2020",
 "Marketing"=>"https://www.mlssoccer.com/",
 "Total Available Impressions"=>"10.0MM",
 "Programming Type"=>"Live - VOD",
 "Demo"=>"P18-49",
 "Recommended"=>"YES",
 "Featured"=>"YES",
 nil=>"10000000"}

但同样的问题出现了:

[22] pry(Program)> row.to_h["Broadcast Name"]
=> nil
[23] pry(Program)> row.to_h["Category"]
=> "Sports,Soccer"
[24] pry(Program)> row.to_h["Sizzle Reel"]
=> "https://youtu.be/fM5aHIVBTIc"

疯狂的是,当我列出所有键时,它会正确显示所有键:

[21] pry(Program)> row.to_h.keys
=> ["Broadcast Name",
 "Description",
 "Category",
 "Sizzle Reel",
 "Thumbnail Image",
 "Header Image",
 "Source",
 "Date",
 "Marketing",
 "Total Available Impressions",
 "Programming Type",
 "Demo",
 "Recommended",
 "Featured",
 nil]

那么,row["Broadcast Name"]不管我做什么,是什么导致如此持续地失败呢?

标签: ruby-on-railsrubycsvruby-on-rails-6

解决方案


它不匹配,因为您的键/标题以不可见字符开头:

row.headers[0].codepoints
#=> [65279, 66, 114, 111, 97, 100, 99, 97, 115, 116, 32, 78, 97, 109, 101]
#    ^^^^^

那是 U+FEFF,或“零宽度无间隔”,用作字节顺序标记

要解决此问题,请剥离 BOM。请参阅如何避免在读取文件时绊倒 UTF-8 BOM


推荐阅读