Hello,
I am a newbie with Ruby and I had a couple of questions while
manipulating csv files. How would I do a multiple column sort? I have
a file that needs to be sorted by the 3rd column, then 1st, then second.
I understand how to do one sort for the csv file but I was unsure of how
to do multiple.
Also,How would I to do a percentage change between two numbers in a
column?
Thank you very much. I know these are easy questions but I am still
learning how to program in Ruby.
On Feb 2, 10:52 am, Michael Sc <michael.schat...@gmail.com> wrote:
Hello,
I am a newbie with Ruby and I had a couple of questions while
manipulating csv files. How would I do a multiple column sort? I have
a file that needs to be sorted by the 3rd column, then 1st, then second.
I understand how to do one sort for the csv file but I was unsure of how
to do multiple.
Also,How would I to do a percentage change between two numbers in a
column?
Thank you very much. I know these are easy questions but I am still
learning how to program in Ruby.
Hello,
I am a newbie with Ruby and I had a couple of questions while
manipulating csv files. How would I do a multiple column sort? I have
a file that needs to be sorted by the 3rd column, then 1st, then second.
I understand how to do one sort for the csv file but I was unsure of how
to do multiple.
Also,How would I to do a percentage change between two numbers in a
column?
Thank you very much. I know these are easy questions but I am still
learning how to program in Ruby.
Come to think of it, I'd do the whole thing using FasterCSV. When
available, I like to use a well known library where the hard work has
already been done for me
require 'rubygems'
require 'faster_csv'
mycsv = FCSV.read("myfile.csv")
FCSV.open("output.csv","w") do |out|
mycsv.sort{|a,b| [a[2],a[0],a[1]] <=> [b[2],b[0],b[1]]}.each |row|
out << row
end
end
I tried outfile.p(doesn't work), outfile.print(takes out the commas) and
outfile.put(takes out the breaks). I know this should be incredibly
easy but I am clearly missing something.
···
On Feb 2, 4:26 pm, Michael Sc <michael.schat...@gmail.com> wrote:
Before writing each record (i.e., each array of fields) to the file,
it must first be converted to a proper csv string.
The 'csv' package has a method for doing this, I presume.
If you don't wan't to use that, try this:
class Array
def to_csv
s = ''
map { |item|
str = item.to_s
# Quote the string if it contains the field-separator or
# a " or a newline or a carriage-return, or if it has leading or
# trailing whitespace.
if str.index( "," ) or /^\s|["\r\n]|\s$/.match(str)
str = '"' + str.gsub( /"/, '""' ) + '"'
end
str
}.join( "," )
end
end
puts [ 674, "yes", "Jones,Tom", 'foo"bar', " space " ].to_csv
--- output -----
674,yes,"Jones,Tom","foo""bar"," space "
···
On Feb 2, 9:53 pm, Michael Sc <michael.schat...@gmail.com> wrote:
William James wrote:
> On Feb 2, 4:26 pm, Michael Sc <michael.schat...@gmail.com> wrote:
>> Thank you very much. I did not realize it was so simple. Do you know
>> how to refer to prior lines to make calculations?
I tried outfile.p(doesn't work), outfile.print(takes out the commas) and
outfile.put(takes out the breaks). I know this should be incredibly
easy but I am clearly missing something.
on the daily return, I wanted to define two if statements so that two
columns had to equal in order to do take the percantage change function.
Does anyone have any ideas on this?
I tried to do this. Thanks again for the help.
on the daily return, I wanted to define two if statements so that two
columns had to equal in order to do take the percantage change function.
Does anyone have any ideas on this?
I tried to do this. Thanks again for the help.
Hey,
I just had another question on these csv files. Is there anyway I could
save sorted files using the fastercsv library? Thanks again for all of
the help. I am having trouble figuring out how to use the libraries.
on the daily return, I wanted to define two if statements so that two columns had to equal in order to do take the percantage change function. Does anyone have any ideas on this?
I tried to do this. Thanks again for the help.
Hey,
I just had another question on these csv files. Is there anyway I could
save sorted files using the fastercsv library? Thanks again for all of
the help. I am having trouble figuring out how to use the libraries.
Assuming your csv file is stored as "my_array":
require 'rubygems'
require 'faster_csv'
FCSV.open("output.csv","w") do |out|
my_array.each{|row| out << row}
end