Created
January 26, 2012 17:56
-
-
Save thejefflarson/1684042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'iconv' | |
def shift | |
line = '' | |
in_quote = 0 | |
loop do | |
if read_line = $stdin.gets("\n") | |
line += read_line.chomp | |
else | |
return nil | |
end | |
csv = [] | |
current = '' | |
line.split(',', -1).each do |cell| | |
cell = Iconv.iconv('ascii//translit//ignore', 'utf-8', cell).first | |
if cell.count('"').zero? && current.empty? | |
csv << cell.squeeze(' ').strip | |
next | |
end | |
in_quote += cell.count '"' | |
current += cell | |
if in_quote % 2 == 0 | |
csv << current.gsub(/(\n|"|\r|,)/, '').dup.squeeze(' ').strip | |
current = '' | |
end | |
end | |
if in_quote % 2 == 0 | |
break csv | |
end | |
end | |
end | |
csv = [] | |
while !csv.nil? | |
csv = shift | |
$stdout.puts(csv.join ",") unless csv.nil? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment