为什么这个数组元素 return 里面有数据的时候会 nil 呢?
Why would this array element return nil when there is data in it?
我正在解析 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">
但是,每当我尝试通过键名访问第一个元素时 returns nil
:
> row["Broadcast Name"]
=> nil
当我尝试通过数组中的索引访问它时 returns 正确的结果:
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"]
无论我做什么都如此失败?
不匹配,因为您的密钥 / header 以不可见字符开头:
row.headers[0].codepoints
#=> [65279, 66, 114, 111, 97, 100, 99, 97, 115, 116, 32, 78, 97, 109, 101]
# ^^^^^
即U+FEFF,或"ZERO WIDTH NO-BREAK SPACE"用作byte order mark。
要解决此问题,请删除 BOM。参见 How to avoid tripping over UTF-8 BOM when reading files。
我正在解析 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">
但是,每当我尝试通过键名访问第一个元素时 returns nil
:
> row["Broadcast Name"]
=> nil
当我尝试通过数组中的索引访问它时 returns 正确的结果:
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"]
无论我做什么都如此失败?
不匹配,因为您的密钥 / header 以不可见字符开头:
row.headers[0].codepoints
#=> [65279, 66, 114, 111, 97, 100, 99, 97, 115, 116, 32, 78, 97, 109, 101]
# ^^^^^
即U+FEFF,或"ZERO WIDTH NO-BREAK SPACE"用作byte order mark。
要解决此问题,请删除 BOM。参见 How to avoid tripping over UTF-8 BOM when reading files。