2017-12-12 5 views
0

배열에서 스프레드 시트를 만들려고합니다. 내가 행의 데이터를 하나의 거대한 행으로 끝낼하지 않도록배열의 X 문자열 양당 새로운 CSV 행을 만드는 방법

#Loop through each .olpOffer (product listing) and gather content from various elements 
parse_page.css('.olpOffer').each do |a| 
    if a.css('.olpSellerName img').empty? 
     seller = a.css('.olpSellerName').text.strip 
    else 
     seller = a.css('.olpSellerName img').attr('alt').value 
    end 
    offer_price = a.css('.olpOfferPrice').text.strip 
    prime = a.css('.supersaver').text.strip 
    shipping_info = a.css('.olpShippingInfo').text.strip.squeeze(" ").gsub!(/(\n)/, '') 
    condition = a.css('.olpCondition').text.strip 
    fba = "FBA" unless a.css('.olpBadge').empty? 

    #Push data from each product listing into array 
    arr.push(seller,offer_price,prime,shipping_info,condition,fba) 
    end 
    #Need to make each product listing's data begin in new row [HELP!!] 
    CSV.open("file.csv", "wb") do |csv| 
     csv << ["Seller", "Price", "Prime", "Shipping", "Condition", "FBA"] 

     end 
    end 

내가 배열이 "FBA" 열 이후에 쓰기 작업을 수행하는 행을 재설정해야 2.

나는 알아낼 수 없습니다 각 문자열을 특정 열 머리글과 상관시키는 방법을 설명합니다. 배열을 사용하지 않아야합니까?

+1

에 의해 항상 나눌 문자열의 일부 주어진 양을 가지고 배열을 =? – mudasobwa

답변

0

나는 그것을 알아 냈다. 나는 배열에서 매 7 개의 문자열 다음에 새로운 행을 만들기 위해 내 CSV에 먹이를주고있는 배열이 필요했습니다. 여기에 어떻게 내가 해냈어 :

편곡 당신이 같이가 입력을 전체 관련없는 구문 분석 코드를 제거하고 제공시겠습니까 7

rows = arr.each_slice(7) 
CSV.open("#{file_name}", "ab") do |csv| 
    csv << [title, asin] 
    rows.each do |row| 
     csv << row 
    end 
end 
관련 문제