I am having a role.csv file as like
Now, I need to update this role.csv file as like below
I tried with FasterCSV and CSV class none of them provide a full access
mode to edit this CSV file.
I have a sample code but thats not looking very effective (but working
Note : CSV file is a sample one (Don’t think Logic in that)
def update_csv_report( emp_id, dep_id, is_accepted )
new_rows = Array.new
CSV.open(‘role.csv’, ‘r’) do |row|
if (row == emp_id && row == dep_id)
new_rows << [row, row, row, row, is_accepted]
new_rows << [row, row, row, row, row]
CSV.open('role.csv', 'w') do |writer| new_rows.each do |row| writer << row end end
I am facing 2 issues in this
- For every updating the CSV file, I am rewriting the whole CSV file.
That not a teddy way to do it.
- I am opening the file first time for reading and 2nd time for
writing. That is also not convincing me.
Please help me in this. How to Improve this code. Main motive is to
updated a particular field instead of rewriting the whole CSV file.
Antony Prabhu N.