TOP

Reading a CSV File with FasterCSV

I needed to import a data in a CSV file into a database. I needed to find the encoding of the CSV file. I found a method to display the encoding in the VIM statusline, and included this code in .vimrc:

# show file encoding in vim status line
if has("statusline")
 set statusline=%< %f\
%h%m%r%=%{\"[\".(&fenc==\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\
&bomb)?\",B\":\"\").\"]\ \"}%k\ %-14.(%l,%c%V%)\ %P
endif

The characterset was Latin1, and I converted it to unicode with Iconv. The row with umlauts had to be split manually, because FasterCSV did not split it.

FasterCSV.foreach(csv_file) do |row|
  if(row[1].nil?)
     row=row[0].split(',')
     Iconv.iconv('UTF-8', 'LATIN1',row[0]).join
     Iconv.iconv('UTF-8', 'LATIN1',row[1]).join
  end
end

Is there a more elegant solution?

Read More

Bad Behavior has blocked 110 access attempts in the last 7 days.