ruby-on-rails - 当其中有数据时,为什么这个数组元素会返回 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"]
不管我做什么,是什么导致如此持续地失败呢?
解决方案
它不匹配,因为您的键/标题以不可见字符开头:
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。
推荐阅读
- javascript - 如何在 .find 方法中引用嵌套对象?
- javascript - 我想在填写所有多步表单后显示预览按钮
- docker - 列出使用 docker 本地卷的 Kubernetes 容器
- python - numba:使用字符串输入/返回向量化函数
- angular - Angular将html转换为角度问题
- python - 将一个目录下所有多种编码的文本文件转换成utf-8编码的文本文件
- arrays - 如何在 C 编程中将一个数组复制到另一个数组
- python - 创建一天中时间的直方图
- node.js - 如何从 express.js 迁移到 nest.js,逐个路由
- c - copy_to_user() 不断打印消息