Dipak Panchal
Dipak Panchal

Reputation: 6036

How to insert a column in the existing CSV file

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

Upvotes: 0

Views: 4822

Answers (3)

scarver2
scarver2

Reputation: 7995

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) }

Upvotes: 5

Ajit Singh
Ajit Singh

Reputation: 146

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.

Upvotes: 0

mikdiet
mikdiet

Reputation: 10018

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

Upvotes: 3

Related Questions