Replacing text in one CSV column using FasterCSV

人走茶凉 提交于 2019-11-29 18:00:05

There's a couple of things you can change in the initialization line that will help. Change:

csv = FCSV.read(filename, :headers => true, :return_headers => true, :encoding => 'u') 

to:

csv = FCSV.read(filename, :headers => true, :encoding => 'u') 

I'm using CSV, which is FasterCSV only it's part of Ruby 1.9. This will create a CSV file in the current directory called "temp.csv" with a modified 'FName' field:

require 'csv'

data = "ID,FName,LName\n1,mickey,mouse\n2,minnie,mouse\n3,donald,duck\n"

# read and parse the data
csv_in = CSV.new(data, :headers => true)

# open the temp file
CSV.open('./temp.csv', 'w') do |csv_out|

  # output the headers embedded in the object, then rewind to the start of the list
  csv_out << csv_in.first.headers
  csv_in.rewind

  # loop over the rows
  csv_in.each do |row|

    # munge the first name
    if (row['FName']['mi'])
      row['FName'] = row['FName'][1 .. -1] << '-' << row['FName'][0] << 'ay'
    end

    # output the record
    csv_out << row.fields
  end
end

The output looks like:

ID,FName,LName
1,ickey-may,mouse
2,innie-may,mouse
3,donald,duck

It is possible to manipulate the desired column directly in the FasterCSV object instead of creating a new column and then trying to replace the old one with the new one.

csv = FCSV.read(filename, :headers => true, :header_converters => :symbol, :return_headers => true, :encoding => 'u')
mycol = csv[:my_col]
mycol.each do |row|
  row.gsub!(/\s*;\s*/,"///") unless row.nil? # or any other substitution
csv.each do |r|
  puts r.to_csv(:force_quotes => true)
end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!