0

I have already CSV file, the content like

a1    a2     a3
1     2      3
4     5      6
5     8      2

Now, What I want, when I read any row i want to add a flag in the csv file like

a1    a2     a3 flag
1     2      3   1
4     5      6   1
5     8      2    

the above flag 1 that means this record is inserted in the table.

so How can I add flag in the csv file?

Thanks In Advance

3 Answers 3

5

I came up with two ways to append a column(s) to an existing CSV file.

Method 1 late merges the new column by reading the file into an array of hashes, then appending the columns to the end of each row. This method can exhibit anomalies if run multiple times.

require 'csv'

filename = 'test.csv'

# Load the original CSV file
rows = CSV.read(filename, headers: true).collect do |row|
  row.to_hash
end

# Original CSV column headers
column_names = rows.first.keys
# Array of the new column headers
additional_column_names = ['flag']
# Append new column name(s)
column_names += additional_column_names
s = CSV.generate do |csv|
  csv << column_names
  rows.each do |row|
    # Original CSV values
    values = row.values
    # Array of the new column(s) of data to be appended to row
    additional_values_for_row = ['1']
    values += additional_values_for_row
    csv << values
  end
end

# Overwrite csv file
File.open(filename, 'w') { |file| file.write(s) }

Method 2 early merges the new column(s) into the row hash. The nicety of this method is it is more compact and avoids duplicate column names if run more than once. This method can also be used to change any existing values in the CSV.

require 'csv'

filename = 'test.csv'

# Load the original CSV file
rows = CSV.read(filename, headers: true).collect do |row|
  hash = row.to_hash
  # Merge additional data as a hash.
  hash.merge('flag' => '0')
  # BONUS: Change any existing data here too!
  hash.merge('a1' => hash['a1'].to_i + 1 )
end

# Extract column names from first row of data
column_names = rows.first.keys
txt = CSV.generate do |csv|
  csv << column_names
  rows.each do |row|
    # Extract values for row of data
    csv << row.values
  end
end

# Overwrite csv file
File.open(filename, 'w') { |file| file.write(txt) }
Sign up to request clarification or add additional context in comments.

Comments

3

You need to write new CSV file with additional column, and then replace original file with new one.

Comments

0

Not sure if you can append a new column in the same file, but you can append a new row into your csv:

CSV.open('your_csv.csv', 'w') do |csv|
  customers.array.each do |row|
    csv << row
  end
end 

Hope this helps.

2 Comments

csv << row, this will add new row in the csv. I want to modify the existing row
Then you have rewrite your file. loop through each line and add/edit/update values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.