From: Haruka YAGNI Date: 2011-04-01T16:50:56+09:00 Subject: Re: SImple question about file io Hi. Paul. I could not reproduce your error, but there is a obvious problem. File open mode "r+" does not create a file if it is not existing. This might not be what you want. Please replace "r+" with "a" (add new lines to the last of a file) and re-try. On Fri, Apr 1, 2011 at 4:31 PM, Paul Joyce wrote: > Hello there > > I am a little frustrated by a simple problem.  This the script below is > failing with > > C:/projects/LAF-01/For-District-Managers/breakout.rb:23:in `block in >
': wrong argument type Object (expected Data) (TypeError) > > This script simple breaks a large file into smaller ones byu the first > column (District).  However, it will not write to the file opf for some > reason.  I have done this so many times - what am Imissing. > > thanks > p > > > data = Array.new > curr = "x" > colhead = "District,Contract No,Contract Status,Tariff,Name,Supply Point > No,Meter No,Meter Make,Address 1,Address 2,Address 3,Address 4,ICS > Feeder Information,Feeder Name,Feeder Meter No,Comments" > > #puts template > ipf = File.new("All-Contracts-29-Mar-11.csv","r") >      ipf.each_line { |line| >      next if line.match(/^$/) >      data = line.split(",") >      if curr == "x"     #  Initialize >        puts "Initialize . . " + data[0] >        opf = File.new(data[0] + '-feeder-customer.csv',"r+") >        opf.puts colhead   # <-----  HERE >      end >      if curr != data[0] and curr != "x" >         puts "New File . . . " + data[0] >         opf.close >         opf = File.new(data[0] + "-feeder-customer.csv","r+") >         opf.puts colhead >      end >      opf.puts line >      curr = data[0] >    } > ipf.close; > opf.close; > > puts 'DONE.' > > -- > Posted via http://www.ruby-forum.com/. > > -- Haruka YAGNI hyagni@gmail.com