Wrong output in fastercsv? Too many " in Ruby

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.

Answers


When you reference @samples you're looking at an array of rows, each of which is an array of parsed fields.

So when you use @samples.each do |a|, the a is a row, and I think you're getting the result of calling #inspect on that row.

What happens if you try something like this?

options = { :headers=>true, :return_headers => true }
@samples = FasterCSV.read(file.path, options).to_a

if @samples.size > 0
  @headers = @samples.shift 
end 

You need :return_headers=>true if you want to get the actual labels; :headers=>true only tells FasterCSV that there are headings - it discards them by default. FasterCSV.read gets and parses all the data in one hit, which is simpler than building the array yourself. Note the to_a at 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 #join instead of #inspect on the headers and add a #join on the rows)?

<%= @headers.join(', ') %>

<ul>
<% @samples.each do |a| %>
<li>
<%= a.join(', ') %>
</li>
<% end %>
</ul>

If that doesn't fix it, hopefully you should at least be further toward understanding what's happening...


Need Your Help

“Cannot support querying” Controller Error

c# asp.net-mvc-4 asp.net-web-api odata web-api

I was working through the following answer found here: Web API OData Inlinecount not working

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.