Reputation: 1581
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!
Upvotes: 0
Views: 1236
Reputation: 37507
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
Upvotes: 1
Reputation: 34031
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.
Upvotes: 1
Reputation: 831
You need to open the files in "append" mode. Like this:
File.open("year_#{row[0]}_info.csv", 'a')
Note the "a".
Upvotes: 2