Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Appending to a CSV file

Reply
Thread Tools

Appending to a CSV file

 
 
Bil Kleb
Guest
Posts: n/a
 
      07-12-2006
Just got a call from a telecommuting colleague
who's using Ruby to automate a bunch of CEV[1]
CFD[2] simulations.

She says that she can't figure out how to /append/
to a CSV file using Ruby's standard library.

Does anyone have the magic recipe, or should
she be using FasterCSV instead?

Thanks,
--
Bil
http://fun3d.larc.nasa.gov

[1] CEV = Crew Exploration Vehicle
[2] CFD = Computational Fluid Dynamics
 
Reply With Quote
 
 
 
 
ChrisH
Guest
Posts: n/a
 
      07-12-2006
Bil Kleb wrote:
....
> She says that she can't figure out how to /append/
> to a CSV file using Ruby's standard library.

....

Wouldn't it just be a matter of opening the file in append mode?

cheers
Chris

 
Reply With Quote
 
 
 
 
Chris Hulan
Guest
Posts: n/a
 
      07-12-2006
ChrisH wrote:
> Bil Kleb wrote:
> ...
>> She says that she can't figure out how to /append/
>> to a CSV file using Ruby's standard library.

> ...
>
> Wouldn't it just be a matter of opening the file in append mode?
>
> cheers
> Chris


Ok, note to self "read docs before commenting"

it seems CSV only supports 'r','rb','w','wb' for open.
FasterCSV delegates to an IO object and supports all IO's modes.
So it would be the winner

Cheers

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bil Kleb
Guest
Posts: n/a
 
      07-12-2006
ChrisH wrote:
> Bil Kleb wrote:
> ...
>> She says that she can't figure out how to /append/
>> to a CSV file using Ruby's standard library.

> ...
>
> Wouldn't it just be a matter of opening the file in append mode?


It would, but unfortunately CSV doesn't support 'a'
or 'a+', viz, lib/ruby/1.8/csv.rb,

83 def CSV.open(path, mode, fs = nil, rs = nil, &block)
84 if mode == 'r' or mode == 'rb'
85 open_reader(path, mode, fs, rs, &block)
86 elsif mode == 'w' or mode == 'wb'
87 open_writer(path, mode, fs, rs, &block)
88 else
89 raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
90 end
91 end

Later,
--
Bil
http://fun3d.larc.nasa.gov
 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      07-12-2006
On Jul 12, 2006, at 1:55 PM, Chris Hulan wrote:

> it seems CSV only supports 'r','rb','w','wb' for open.
> FasterCSV delegates to an IO object and supports all IO's modes.
> So it would be the winner


Interestingly, this design choice in FasterCSV has been questioned in
the past. I just keep liking it more and more though as issues like
this arise.

James Edward Gray II


 
Reply With Quote
 
Chris Hulan
Guest
Posts: n/a
 
      07-12-2006
James Gray wrote:
> On Jul 12, 2006, at 1:55 PM, Chris Hulan wrote:
>
>> it seems CSV only supports 'r','rb','w','wb' for open.
>> FasterCSV delegates to an IO object and supports all IO's modes.
>> So it would be the winner

>
> Interestingly, this design choice in FasterCSV has been questioned in
> the past. I just keep liking it more and more though as issues like
> this arise.
>
> James Edward Gray II


It does seem a bit odd that CSV limits the modes.

On the other hand, the desired result can be achieved by creating a
new file, copy existing data, add the new data, delete (or rename to be
safe)
the original and rename the new file to the old file name.

I think supporting the append mode is easier all round...

cheers
Chris

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      07-13-2006
Bil Kleb wrote:
> ChrisH wrote:
>
>> Bil Kleb wrote:
>> ...
>>
>>> She says that she can't figure out how to /append/
>>> to a CSV file using Ruby's standard library.

>>
>> ...
>>
>> Wouldn't it just be a matter of opening the file in append mode?

>
>
> It would, but unfortunately CSV doesn't support 'a'
> or 'a+', viz, lib/ruby/1.8/csv.rb,


Well, darn.

All I can think of offhand is 1) write the new stuff to a new file
and then 2) use fileutils to append the new file onto the old one.

Disclaimers:

1. I'm not sure fileutils has a good way to do that, but
if it doesn't, there's always system("cat newfile >>oldfile").

2. Obviously be sure the old file is closed before trying to
so anything funny with it.


Hal

 
Reply With Quote
 
Seth Thomas Rasmussen
Guest
Posts: n/a
 
      07-13-2006
I'm not very familiar with the CSV lib, but if the heart of the matter
is appending to a CSV file, you hardly need a full-on CSV lib for
something like that.

File.open(my_csv) {|f| f.puts new_record} # think it oughta be as
simple as that
File.open(my_csv) {|f| f.puts *new_records} # puts can take a list, too

Bil Kleb wrote:
> ChrisH wrote:
> > Bil Kleb wrote:
> > ...
> >> She says that she can't figure out how to /append/
> >> to a CSV file using Ruby's standard library.

> > ...
> >
> > Wouldn't it just be a matter of opening the file in append mode?

>
> It would, but unfortunately CSV doesn't support 'a'
> or 'a+', viz, lib/ruby/1.8/csv.rb,
>
> 83 def CSV.open(path, mode, fs = nil, rs = nil, &block)
> 84 if mode == 'r' or mode == 'rb'
> 85 open_reader(path, mode, fs, rs, &block)
> 86 elsif mode == 'w' or mode == 'wb'
> 87 open_writer(path, mode, fs, rs, &block)
> 88 else
> 89 raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
> 90 end
> 91 end
>
> Later,
> --
> Bil
> http://fun3d.larc.nasa.gov


 
Reply With Quote
 
Seth Thomas Rasmussen
Guest
Posts: n/a
 
      07-13-2006
Sorry about my last post sort of skipping over the whole matter of the
write mode.. you're smarter than me, though, I'm sure you'll be fine.
;-p


Bil Kleb wrote:
> ChrisH wrote:
> > Bil Kleb wrote:
> > ...
> >> She says that she can't figure out how to /append/
> >> to a CSV file using Ruby's standard library.

> > ...
> >
> > Wouldn't it just be a matter of opening the file in append mode?

>
> It would, but unfortunately CSV doesn't support 'a'
> or 'a+', viz, lib/ruby/1.8/csv.rb,
>
> 83 def CSV.open(path, mode, fs = nil, rs = nil, &block)
> 84 if mode == 'r' or mode == 'rb'
> 85 open_reader(path, mode, fs, rs, &block)
> 86 elsif mode == 'w' or mode == 'wb'
> 87 open_writer(path, mode, fs, rs, &block)
> 88 else
> 89 raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
> 90 end
> 91 end
>
> Later,
> --
> Bil
> http://fun3d.larc.nasa.gov


 
Reply With Quote
 
gregarican
Guest
Posts: n/a
 
      07-13-2006
I haven't used the CSV module before either, but looking into it this
test script I came up with parses a CSV source file, writing it to a
new array. Then you can append to that new array and write the results
back to the original CSV file. It's not pretty and might not be the
most Rubyish way of getting the job done but it worked for me using
Ruby 1.8.2 on Win32. Try it out and let me know...

require 'csv'

# test.csv consists of three comma delimited fields, something like:
#
# 1,"Greg","Admin"
# 2,"Joe","User"
# 3,"Jim","Admin"

new_array = []
temp_array = []

reader = CSV.open('test.csv', 'r') do |row|
(0...row.nitems).each do |item|
temp_array.push(row[item].data)
end
new_array << temp_array
temp_array = []
end

new_items = [4,"Jane","User"]
new_array << new_items

CSV.open('test.csv', 'w') do |writer|
new_array.each do |row|
writer << row
end
end


Bil Kleb wrote:
> ChrisH wrote:
> > Bil Kleb wrote:
> > ...
> >> She says that she can't figure out how to /append/
> >> to a CSV file using Ruby's standard library.

> > ...
> >
> > Wouldn't it just be a matter of opening the file in append mode?

>
> It would, but unfortunately CSV doesn't support 'a'
> or 'a+', viz, lib/ruby/1.8/csv.rb,
>
> 83 def CSV.open(path, mode, fs = nil, rs = nil, &block)
> 84 if mode == 'r' or mode == 'rb'
> 85 open_reader(path, mode, fs, rs, &block)
> 86 elsif mode == 'w' or mode == 'wb'
> 87 open_writer(path, mode, fs, rs, &block)
> 88 else
> 89 raise ArgumentError.new("'mode' must be 'r', 'rb', 'w', or 'wb'")
> 90 end
> 91 end
>
> Later,
> --
> Bil
> http://fun3d.larc.nasa.gov


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read CSV file using csv library Li Chen Ruby 18 03-23-2010 12:44 AM
read and write csv file using csv module jliu66 Python 0 10-19-2007 03:12 PM
How to move data from a CSV file to a JTable, and from a JTable to a CSV file ? Tintin92 Java 1 02-14-2007 06:51 PM
Re: csv writerow creates double spaced excel csv files Skip Montanaro Python 0 02-13-2004 08:50 PM
csv writerow creates double spaced excel csv files Michal Mikolajczyk Python 0 02-13-2004 08:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57