I am implementing in Ruby, and I just want to read in a CSV-file. My CSV looks like :
"official_code","username","lastname","firstname","email","Course Groups","password"
"1626","000078sr","LENS","Ser","blab@hogent.be","project1g43","pwd7975"
"7334","000116jd","DE ","Joen","je0116@.hogent.be","project1g08","pwd5259"
"2003","000136bv","VACKE","Bert","hogent.be","project1g18","pwd5908"
"4065","000166cr","ROHøJ","Chrian","chrhogent.be","project1g10","pwd5000"
It is made in Excel and set in a CSV-file
But when I do in my application:
FasterCSV.foreach(file.path, {:headers=>true }) do |row|
@samples[i] = row
i += 1
end # do
if @samples.size > 0
@headers = @samples[0].headers
end
and in my view I just do:
<%= @headers.inspect %>
<ul>
<% @samples.each do |a| %>
<li>
<%= a %>
</li>
<% end %>
</ul>
I get:
["\"official_code\",\"username\",\"lastname\",\"firstname\",\"email\",\"Course Groups\",\"password\""]
"""1626"",""000078sr"",""ENS"",""Sder"",""san.hogent.be"",""project1g43"",""pwd7975"""
"""7334"",""000116jd"",""DE VOS"",""Jen"",""jeroen.ogent.be"",""project1g08"",""pwd5259"""
"""2003"",""000136bv"",""VYCKE"",""Bet"",""berent.hogent.be"",""project1g18"",""pwd5908"""
"""4065"",""000166cr"",""ROøJ"",""Chstian"",""chr.hogent.be"",""project1g10"",""pwd5000"""
So there are way to many ” ‘s. Does someone know what I am doing wrong? It is just like rows only have 1 column. If I do samples[0].each I get only 1 item. The whole row and not the columns.
When you reference
@samplesyou’re looking at an array of rows, each of which is an array of parsed fields.So when you use
@samples.each do |a|, theais a row, and I think you’re getting the result of calling#inspecton that row.What happens if you try something like this?
You need
:return_headers=>trueif you want to get the actual labels;:headers=>trueonly tells FasterCSV that there are headings – it discards them by default.FasterCSV.readgets and parses all the data in one hit, which is simpler than building the array yourself. Note theto_aat the end, which forces the result to really be an array – it might make things simpler in the next part.What happens if you modify the view as follows (use
#joininstead of#inspecton the headers and add a#joinon the rows)?If that doesn’t fix it, hopefully you should at least be further toward understanding what’s happening…