Ruby on rails 为什么这个数组元素中有数据时会返回nil?

Ruby on rails 为什么这个数组元素中有数据时会返回nil?,ruby-on-rails,ruby,csv,ruby-on-rails-6,Ruby On Rails,Ruby,Csv,Ruby On Rails 6,我正在分析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

我正在分析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">
当我尝试通过数组中的索引访问它时,它会返回正确的结果:

 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.headers[0].codepoints
#=> [65279, 66, 114, 111, 97, 100, 99, 97, 115, 116, 32, 78, 97, 109, 101]
#    ^^^^^
这就是U+FEFF,或“零宽度无中断空间”,用作


要解决此问题,请剥离BOM表。请参阅。

寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定问题或错误以及在问题本身中重现该问题所需的最短代码。请参阅:“”和链接页面。我无法在您发布的哈希上复制。理论上,它可能是一个看起来相似的不同字符串,这是由于外观相似、不同的规范化状态或不可见字符。例如,
“Brοadcast Name”!=“广播名称”
,因为第一个包含“希腊文小写字母OMICRON”(U+03BF),字母
o
应该在其中。比较
str.codepoints
而不是依赖于渲染字体的视觉确认。特别是,在阅读bommy文件时使用。真棒。谢谢你的发现。阅读该文件时,如何剥离BOM表?@marcamillion您的评论上方有一条评论:-)刚刚看到,但要将答案标记为完整答案,其他人必须完整(因此他们不必在评论中搜索)。我已包含指向其他答案的链接。
row.headers[0].codepoints
#=> [65279, 66, 114, 111, 97, 100, 99, 97, 115, 116, 32, 78, 97, 109, 101]
#    ^^^^^