Skip to content

Instantly share code, notes, and snippets.

@mitio
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitio/cc0f2b8aeac628e7fae3 to your computer and use it in GitHub Desktop.
Save mitio/cc0f2b8aeac628e7fae3 to your computer and use it in GitHub Desktop.
require 'csv'
CSV.read('file.csv', headers: true).each { |row| row['име на колонката от хедъра'] }
#!/usr/bin/ruby
require 'csv'
if ARGV.empty?
STDERR.puts "Usage: #{__FILE__} path-to-file.csv"
STDERR.puts 'The normalized CSV file will be printed on STDOUT.'
exit 1
end
def append_cell_values_of(row_a, row_b)
row_a.zip(row_b).map do |cell_a_value, cell_b_value|
"#{cell_a_value}\n#{cell_b_value}"
end
end
normalized_rows = []
CSV.read(ARGV.first).each do |row|
if row.first.nil?
normalized_rows[-1] = append_cell_values_of(normalized_rows.last, row)
else
normalized_rows << row
end
end
# Some cells might have unnecessary newlines at the beginning or at the
# end as a result of the normalization, so we strip these away.
normalized_rows = normalized_rows.map do |row|
row.map do |cell|
cell.strip if cell
end
end
puts CSV.generate { |csv| normalized_rows.each { |row| csv << row } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment