lib/slicing.rb in slicing-0.1.1 vs lib/slicing.rb in slicing-0.1.2.beta1

- old
+ new

@@ -7,10 +7,33 @@ class Base < Thor check_unknown_options! package_name 'slicing' default_task :help + + desc :produce, "produce output.csv with the column value equal to given value" + def produce path, column_name, value, output + index = 0 + str = "" + CSV.foreach(path, :headers => true, encoding: "ISO8859-1:utf-8") do |row| + str = row + break + end + index = str.index(column_name) + answer = 0 + CSV.open(output, "a+") do |csv| + CSV.foreach(path) do |row| + csv << row if row[index] == value + end + end + end + + desc :clean, "clean up by removing rows with column value" + def clean path, output, name, value + # puts "add header" + end + # desc :gsub, "" # def gsub path, output, first, second # CSV.foreach(path,:headers=> true, :encoding => "ISO8859-1:utf-8") do |row| # puts row # row.map {|n| n.gsub(first,second) if n !=nil} @@ -29,15 +52,84 @@ # csv << row # end # end # end - desc :clean, "clean up by removing rows with column value" - def clean path, output, name, value - # puts "add header" + desc :cat, "cat two csv files and keep the headers using the first csv" + def cat path, path_column, path2, path2_column, output + end + desc :combine, "combine" + def combine path, path_column, path2, path2_column, output + + end + + desc :append, "append" + def append path, output, value + data_to_merge = CSV.read(path,:headers=> true, :encoding => "ISO8859-1:utf-8") #TODO: is this a data + CSV.open(output, "a+") do |csv| + data_to_merge.each_with_index do |data,index| + csv << data.push(value) + end + end + end + + desc :keep, "keep the columns" + def keep path, output, *column + data = CSV.read(path, :headers=> true, :encoding => "ISO8859-1:utf-8") #2014 + header = data[0] + column_array = [] + column.size.times do |index| + column_array.push(header.index(column[index])) + end + CSV.open(output,"a+") do |csv| + data.each_with_index do |row,index| + array = [] + column.size.times do |value| + array.push(row[column[value]]) + end + csv << array + end + end + end + + + desc :equal, "equal " + def equal path, column_name, value + index = 0 + str = "" + CSV.foreach(path, :headers => true, encoding: "ISO8859-1:utf-8") do |row| + str = row + break + end + # array = str.to_s.split(",") + index = str.index(column_name) + #get the number + answer = 0 + CSV.foreach(path) do |row| + answer = answer + 1 if row[index] == value + end + puts answer + end + + + + desc :remove, "remove a header" + def remove path, output + index = 0 + CSV.foreach(path) do |row| + CSV.open(output, "a+") do |csv| + if index != 0 + csv << row + end + end + index = index +1 + end + end + + desc :add, "add a header" def add path, output, *headers index = 0 CSV.foreach(path) do |row| CSV.open(output, "a+") do |csv| @@ -196,10 +288,12 @@ puts row puts "----" puts "#{row.count} columns" puts "----" print_header(row) + puts "----" + print_header_with_quote(row) exit end end desc :unique, "calculate number of unique values in column" @@ -216,18 +310,19 @@ puts "#{data.count} rows #{data[0].count} columns" puts "---" puts "#{data[0]}" puts "---" print_header(data[0]) + puts "---" + print_header_with_quote(data[0]) end desc :subset, "create a subset of the data" method_option :line, type: :numeric, aliases: '-l', default: 1000 def subset(csv_file, output) path = csv_file output_directory = output #"/Users/ytbryan/Desktop/output/subset-2015.csv" #output directory - # options[:num] == nil ? (stop = 10) : (stop = options[:num]) stop = options[:line] counter = 0 CSV.foreach(path, :headers => false, encoding: "ISO8859-1:utf-8") do |row| exit if counter == stop begin @@ -238,33 +333,18 @@ rescue end end end - # desc :subsetagain, "" - # def subsetagain csv_file, output, value=10 - # path = csv_file - # output_directory = output #"/Users/ytbryan/Desktop/output/subset-2015.csv" #output directory - # stop = value - # counter = 0 - # CSV.foreach(path, :headers => false, :row_sep => "\r\n", encoding: "ISO8859-1:utf-8") do |row| - # exit if counter == stop - # begin - # counter = counter + 1 - # CSV.open(output_directory, "a+") do |csv| - # csv << row - # end - # rescue - # end - # end - # end - private def print_header array puts array.join(",") if array != nil end + def print_header_with_quote array + puts "#{"array.join("' '")"}" if array != nil + end def process_options headers, rowsep, utf if headers == nil headers = true else headers = headers