0

test1.csv

yearID,teamID,lgID,playerID,salary
1985,BAL,AL,boddimi01,625000
1985,BAL,AL,dauerri01,480000
1985,BAL,AL,davisst02,437500
1986,BAL,AL,dempsri01,512500
1986,BAL,AL,dwyerji01,375000
1987,BAL,AL,flanami01,641667

This is my ruby code!

File.foreach('test1.csv') do |csv_line|
    row = CSV.parse_line(csv_line)
    
    if File.exist?("year_#{row[0]}_info.csv")
        File.open("year_#{row[0]}_info.csv", 'w') do |f|     
            f.write("\n#{row[4]}")   
        end
    else
        File.open("year_#{row[0]}_info.csv", 'w') do |f|     
            f.write("#{row[4]}")   
        end
    end
end

I am trying to get one of the following output

#year_1985_info.csv
625000
480000
437500

But I am only getting this output

#year_1985_info.csv

437500

How do I get the desired output?

Thanks a lot in advance!

Community
  • 1
  • 1
gkolan
  • 1,571
  • 2
  • 20
  • 37

3 Answers3

2

You need to open the files in "append" mode. Like this:

File.open("year_#{row[0]}_info.csv", 'a')

Note the "a".

mchail
  • 821
  • 5
  • 12
  • Thanks for the answer. I changed from w to w+ but output has not changed. – gkolan Apr 19 '13 at 01:26
  • My mistake! You actually need 'a' mode. I'm updating the answer. I just tested this on my machine and it works with 'a' mode. – mchail Apr 19 '13 at 01:30
1

If the file exists, you want to append to it rather than creating an empty file:

require 'csv'

File.foreach('test1.csv') do |csv_line|
  row = CSV.parse_line(csv_line)

  ofname = "year_#{row[0]}_info.csv";
  if File.exist?(ofname)
    File.open(ofname, 'a') do |f|       # Note the 'a' here
      f.write("\n#{row[4]}")
    end
  else
    File.open(ofname, 'w') do |f|
      f.write("#{row[4]}")
    end
  end
end

I actually think it's better to have a newline at the end of every line; this makes the CSV file easier to work with. It also simplifies your code:

require 'csv'

File.foreach('test1.csv') do |csv_line|
  row = CSV.parse_line(csv_line)

  File.open("year_#{row[0]}_info.csv", 'a') do |f|
    f.write("#{row[4]}\n")
  end
end

Note that the 'a' is actually fine for creating or appending.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
1

It's inefficient to keep opening and closing the same file.

What I would do is group them by year and then print them to each file all at once.

scores = CSV.read('test1.csv').drop(1) #drop header line
grouped = scores.group_by(&:first)     #group by year

grouped.each do |year, rows|
  File.open("year_#{year}_info.csv", "w") do |f|
    f.puts rows.map(&:last)  #just want last column
  end
end
Mark Thomas
  • 37,131
  • 11
  • 74
  • 101