Ruby on rails Rails创建大型CSV文件的最佳方法?
创建大型CSV文件的最佳方法是什么 我有一个用nokogiri解析的XML文件,现在我需要生成一个CSV文件,用于将产品导入Magento。大概有40列,我只需要填写大约10列 我丑陋的解决方案:Ruby on rails Rails创建大型CSV文件的最佳方法?,ruby-on-rails,ruby,csv,Ruby On Rails,Ruby,Csv,创建大型CSV文件的最佳方法是什么 我有一个用nokogiri解析的XML文件,现在我需要生成一个CSV文件,用于将产品导入Magento。大概有40列,我只需要填写大约10列 我丑陋的解决方案: require 'nokogiri' csv_text = File.open("#{Dir.pwd}/app/controllers/ps_product.xml") @csv = Nokogiri::XML.parse(csv_text) CSV.open("#{Dir.p
require 'nokogiri'
csv_text = File.open("#{Dir.pwd}/app/controllers/ps_product.xml")
@csv = Nokogiri::XML.parse(csv_text)
CSV.open("#{Dir.pwd}/app/controllers/super.csv", "w+") do |csv|
csv << [%q(sku,_store,_attribute_set,_type,_category,_root_category,_product_websites,color,cost,description,length,meta_description,meta_keyword,meta_title,name,price,short_description,status,url_key,visibility,weight,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock,notify_stock_qty,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,stock_status_changed_auto,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,_links_related_sku,_links_related_position,_links_crosssell_sku,_links_crosssell_position,_links_upsell_sku,_links_upsell_position,_associated_sku,_associated_default_qty,_associated_position,_tier_price_website,_tier_price_customer_group,_tier_price_qty,_tier_price_price,_group_price_website,_group_price_customer_group,_group_price_price,_media_attribute_id,_media_image,_media_lable,_media_position,_media_is_disabled)]
@csv.xpath("//table").each do |produkt|
text = produkt.xpath('*[@name="reference"]').text + ',,"Hair extensions ",simple,"","Default Category",base,"Sort, farve nr. 1",,'
produkt.xpath('*[@name="description"]').text + ',"65 cm",'
produkt.xpath('*[@name="meta_description"]').text + ','
produkt.xpath('*[@name="meta_keywords"]').text + ','
produkt.xpath('*[@name="meta_title"]').text + ','
produkt.xpath('*[@name="name"]').text + ','
produkt.xpath('*[@name="price"]').text + ','
produkt.xpath('*[@name="short_description"]').text + ',1,'
produkt.xpath('*[@name="link_rewrite"]').text + ',4,'
produkt.xpath('*[@name="weight"]').text + ','
produkt.xpath('*[@name="quanity"]').text + ',0.0000,1,0,0,1,1.0000,1,0.0000,1,0,,1,0,1,0,1,0.0000,1,0,0,,,,,,,,,,,,,,,,,88,,,1,0'
csv << text
end
需要“nokogiri”
csv_text=File.open(“#{Dir.pwd}/app/controllers/ps_product.xml”)
@csv=Nokogiri::XML.parse(csv_文本)
打开(“#{Dir.pwd}/app/controllers/super.CSV”,“w+”)do|CSV|
csv将尝试csv生成器。没有什么不好的。这是一项简单的任务——将列逐个连接到字符串中并将其存储在文件中。使用Gsub方法还可以使其更美观。