我有一个产品文件,列出了商品编号、产品和价格。我想读取此文件并将其初始化为散列,其中项目 # 为键,产品和价格为值。这是我的文件
199, Shoes, 59.99
211, Shirts, 19.99
245, Hats, 25.99
689, Coats, 99.99
712, Beanies, 6.99
我希望它看起来像这样。
products = {
199 =>['Shoes', 59.99],
211 =>['Shirts', 19.99],
245 =>['Hats', 25.99],
689 => ['Coats', 99.99],
712 => ['Beanies', 6.99]
}
这是我能想到的,但这并不是它真正想要的。
products_file = File.open("files.txt")
products_hash = []
while ! products_file.eof?
product_hash = products_file.gets.chomp
print product_hash.split(', ')
end
这是我想出的输出:
["199", "Shoes", "59.99"]
["211", "Shirts", "19.99"]
["245", "Hats", "25.99"]
["689", "Coats", "99.99"]
["712", "Beanies", "6.99"]