2010-05-12 12 views
9

मैं CS12 फ़ाइल को पार्स करने और डेटाबेस तालिका में सामग्री जोड़ने के लिए this tutorial से कोड का उपयोग कर रहा हूं। मैं CSV फ़ाइल की पहली पंक्ति को कैसे अनदेखा करूं? नियंत्रक कोड के नीचे है:सीएसवी पार्स रेल पर पहली पंक्ति को अनदेखा करें

def csv_import 
    @parsed_file=CSV::Reader.parse(params[:dump][:file]) 
    n = 0 
    @parsed_file.each do |row| 
    s = Student.new 
    s.name = row[0] 
    s.cid = row[1] 
    s.year_id = find_year_id_from_year_title(row[2]) 
    if s.save 
     n = n+1 
     GC.start if n%50==0 
    end 
    flash.now[:message] = "CSV Import Successful, #{n} new students added to the database." 
    end 
    redirect_to(students_url) 
end 

उत्तर

11
@parsed_file.each_with_index do |row, i| 
    next if i == 0 
    .... 
+0

आदेश एन अतिरिक्त तुलना – baash05

41

यह सवाल अप पॉपिंग जब मैं कैसे सीएसवी/FasterCSV पुस्तकालयों के साथ पहली पंक्ति को छोड़ने के लिए खोज रहा था रखा है, तो यहां समाधान कि यदि आप अंत है यहाँ।

समाधान है ... CSV.foreach("path/to/file.csv",{:headers=>:first_row}) do |row|

HTH।

+5

आप दूसरे पैरामीटर के रूप में हैश '{: headers => true}' को भी पास कर सकते हैं। यह भी काम करता है। –

3

यदि आप शीर्षकों के रूप में अपनी पहली पंक्ति की पहचान करते हैं तो आप एक सरल Array के बजाय ऑब्जेक्ट वापस प्राप्त करते हैं।

जब आप सेल मान लेते हैं, ऐसा लगता है कि आपको ऑब्जेक्ट पर .fetch("Row Title") का उपयोग करने की आवश्यकता है।

यही वह है जो मैं आया था। मैं nil को if सशर्त के साथ छोड़ रहा हूं।

CSV.foreach("GitHubUsersToAdd.csv",{:headers=>:first_row}) do |row| username = row.fetch("GitHub Username") if username puts username.inspect end end

0
require 'csv' 
csv_content =<<EOF 
lesson_id,user_id 
5,3 
69,95 
EOF 

parse_1 = CSV.parse csv_content 
parse_1.size # => 3 # it treats all lines as equal data 

parse_2 = CSV.parse csv_content, headers:true 
parse_2.size # => 2 # it ignores the first line as it's header 

parse_1 
# => [["lesson_id", "user_id"], ["5", "3"], ["69", "95"]]  
parse_2 
# => #<CSV::Table mode:col_or_row row_count:3> 
यहाँ

जहां यह मजेदार हिस्सा

parse_1.each do |line| 
    puts line.inspect  # the object is array 
end 
# ["lesson_id", "user_id"] 
# ["5", " 3"] 
# ["69", " 95"] 


parse_2.each do |line| 
    puts line.inspect  # the object is `CSV::Row` objects 
end 
# #<CSV::Row "lesson_id":"5" "user_id":" 3"> 
# #<CSV::Row "lesson_id":"69" "user_id":" 95"> 
तो इसलिए

मैं क्या कर सकते हैं

parse_2.each do |line| 
    puts "I'm processing Lesson #{line['lesson_id']} the User #{line['user_id']}" 
end 
# I'm processing Lesson 5 the User 3 
# I'm processing Lesson 69 the User 95 
0
data_rows_only = csv.drop(1) 

यह

करना होगा
csv.drop(1).each do |row| 
    # ... 
end 

इच्छा पाश यह

0

इस सरल कोड का उपयोग करना, आप एक CSV फ़ाइल पढ़ सकते हैं और पहली पंक्ति जो शीर्ष लेख या फ़ील्ड नाम है अनदेखा कर सकते हैं:

CSV.foreach(File.join(File.dirname(__FILE__), filepath), headers: true) do |row| 
    puts row.inspect 
end 

आप आप क्या कभी आप चाहते क्या कर सकते हैं row के साथ। headers: true

संबंधित मुद्दे