Last active
August 29, 2015 14:05
-
-
Save mitio/cc0f2b8aeac628e7fae3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'csv' | |
CSV.read('file.csv', headers: true).each { |row| row['име на колонката от хедъра'] } |
This file contains hidden or 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
#!/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