在 ruby?? 3.1.2 中,使用這個 CSV 檔案:
make,model,color,doors,email
dodge,charger,black,4,practice1@whatever.com
ford,focus,blue,5,practice2@whatever.com
nissan,350z,black,2,practice3@whatever.com
mazda,miata,white,2,practice4@whatever.com
honda,civid,brown,4,practice5@whatever.com
corvette,stingray,red,2,practice6@whatever.com
ford,fiesta,blue,5,practice7@whatever.com
bmw,m4,black,2,practice8@whatever.com
audi,a5,blue,2,practice9@whatever.com
subaru,brz,black,2,practice10@whatever.com
lexus,rc,black,2,practice11@whatever.com
我如何才能列印出特定于電子郵件的行之一。例如,用戶輸入:“ [email protected]”,輸出如下:lexus,rc,black,2,[email protected]
uj5u.com熱心網友回復:
csv = CSV.read('filename.csv', headers: true)
row = csv.find {|row| row['email'] == '[email protected]'}
=> #<CSV::Row "make":"lexus" "model":"rc" "color":"black" "doors":"2" "email":"[email protected]">
然后根據需要將輸出映射到模型。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/516601.html
標籤:红宝石CSV
上一篇:合并一個Ruby哈希陣列
下一篇:包含模塊Ruby上的未定義方法
