Last active
August 29, 2015 14:16
-
-
Save boriscy/6366fb5cf52ccccada88 to your computer and use it in GitHub Desktop.
Convert ranges from old format to new
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
# convert from | |
# (0 - 21 - 71 - 121) : (0 - 0,491 - 0,563 - 0,66) | |
# to | |
# (0-20:0); (21-70:0.491); (71-120:0.563); (121:0.66) | |
require 'csv' | |
module ConvertRange | |
def self.convert(str) | |
ranges, values = str.split(":") | |
ranges.gsub!(/[^\d-]/, '') | |
values.gsub!(/[\s\(\)]/, '') | |
arr = ranges.split("-").map(&:to_i) | |
arr = arr.each_with_index.map { |val, i| [val, arr[i + 1].to_i - 1] } | |
lst = arr.pop | |
arr = arr.map { |a, b| "#{a}-#{b}" } | |
arr << "#{ lst.first }" | |
arr.zip(values.gsub(/,/, '.').split("-")).map { |a, b| "(#{a}:#{b})"}.join("; ") | |
end | |
end | |
["cessa", "lpz", "cre", "elfec", "endebeni", "elfeo", "sepsa", "setar"].each do |file| | |
path = "/home/boris/Desktop/#{ file }.csv" | |
next unless File.exists?(path) | |
puts "converting #{file}" | |
arr = CSV.read(path) | |
str = CSV.generate do |csv| | |
arr.each do |row| | |
[11, 12, 13, 14].each do |pos| | |
row[pos] = ConvertRange.convert(row[pos]) if !row[pos].nil? && row[pos].strip != "" | |
end | |
csv << row | |
end | |
end | |
f = File.new("/home/boris/Desktop/#{ file }_mod.csv", "w+") | |
f.write(str) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment