使用FasterCSV替换一个CSV列中的文本 [英] Replacing text in one CSV column using FasterCSV

查看:74
本文介绍了使用FasterCSV替换一个CSV列中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相对于Ruby来说,我还比较陌生,所以我试图找出如何使用FasterCSV进行以下操作: 打开一个CSV文件,在其标题中选择一列,在此列中,仅将所有出现的字符串x替换为y,然后将新文件写到STDOUT中. 以下代码几乎可以正常工作:

Being relatively new to Ruby, I am trying to figure out how to do the following using FasterCSV: Open a CSV file, pick a column by its header, in this column only replace all occurrences of string x with y, write out the new file to STDOUT. The following code almost works:

filename = ARGV[0]
csv = FCSV.read(filename, :headers => true, :header_converters => :symbol, :return_headers => true, :encoding => 'u') 
mycol = csv[:mycol]
# construct a mycol_new by iterating over mycol and doing some string replacement
puts csv[:mycol][0] # produces "MyCol" as expected
puts mycol_new[0] # produces "MyCol" as expected
csv[:mycol] = mycol_new
puts csv[:mycol][0] # produces "mycol" while "MyCol" is expected
csv.each do |r|
  puts r.to_csv(:force_quotes => true)
end

唯一的问题是在我不期望的地方有标头转换.如果在替换csv表中的列之前所选列的标题为"MyCol",则其后为"mycol"(请参阅​​代码中的注释).为什么会这样?以及如何避免呢?谢谢.

The only problem is that there is a header conversion where I do not expect it. If the header of the chosen column is "MyCol" before the substitution of the columns in the csv table it is "mycol" afterwards (see comments in the code). Why does this happen? And how to avoid it? Thanks.

推荐答案

您可以在初始化行中进行一些更改,这将有所帮助.更改:

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

收件人:

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

我正在使用CSV,这是FasterCSV,仅它是Ruby 1.9的一部分.这将在当前目录中创建一个名为"temp.csv"的CSV文件,并带有修改后的"FName"字段:

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

输出如下:

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

这篇关于使用FasterCSV替换一个CSV列中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆