如何解析CSV文件,更新字段,然后保存

时间:2021-04-27 13:37:31

I need to read a CSV file, update a field, then save the changes. I have everything working fine except saving my changes to the field I'm updating:

我需要读取CSV文件,更新字段,然后保存更改。除了将更改保存到我正在更新的字段之外,我的一切工作正常:

require 'csv'
@parsed_file = CSV::Reader.parse(File.open("#{RAILS_ROOT}/doc/some.csv"))
@parsed_file.each_with_index  do |row, x|
  address = row[5]
  l = Location.address_find(address)
  if l != nil
    puts "#{l.name} at #{l.address}"
    row[14] = l.store_code
    puts row[14]
  else
    puts "No matching address Found!!!"
  end
  #What do I do here? Something like this? CSV::Writer.generate(@parsed_file) 
end

What do I do here? How do I save the changes I made and update the file?

我该怎么办?如何保存我所做的更改并更新文件?

1 个解决方案

#1


11  

What I would do is write the updated records to a new file and then, if you want, after you have finished your program can delete the old file and rename the new file to have the original file name.

我要做的是将更新的记录写入新文件,然后,如果需要,在完成程序后可以删除旧文件并将新文件重命名为原始文件名。

To do this I would open the output file at the top of your code, outside the each_with_index loop. e.g.

为此,我将在each_with_index循环之外的代码顶部打开输出文件。例如

csv_out = CSV::Writer.generate(File.open('new.csv', 'wb'))

Then inside your each_with_index loop you can write the current row to the new file like this:

然后在each_with_index循环中,您可以将当前行写入新文件,如下所示:

csv_out << row

Then at the end you can close the new file:

然后在最后你可以关闭新文件:

csv_out.close

As mentioned in the comments, CSV::Writer is no longer in the standard library. An equivalent version of the code using the newer CSV.foreach (for reading) and CSV.open (for writing) is:

如评论中所述,CSV :: Writer不再位于标准库中。使用较新的CSV.foreach(用于阅读)和CSV.open(用于写入)的代码的等效版本是:

CSV.open("path/to/file.csv", "wb") do |csv_out|
  CSV.foreach("#{RAILS_ROOT}/doc/some.csv") do |row|
    address = row[5]
    l = Location.address_find(address)
    if l != nil
      puts "#{l.name} at #{l.address}"
      row[14] = l.store_code
      puts row[14]
    else
      puts "No matching address Found!!!"
    end
    csv_out << row 
  end
end

#1


11  

What I would do is write the updated records to a new file and then, if you want, after you have finished your program can delete the old file and rename the new file to have the original file name.

我要做的是将更新的记录写入新文件,然后,如果需要,在完成程序后可以删除旧文件并将新文件重命名为原始文件名。

To do this I would open the output file at the top of your code, outside the each_with_index loop. e.g.

为此,我将在each_with_index循环之外的代码顶部打开输出文件。例如

csv_out = CSV::Writer.generate(File.open('new.csv', 'wb'))

Then inside your each_with_index loop you can write the current row to the new file like this:

然后在each_with_index循环中,您可以将当前行写入新文件,如下所示:

csv_out << row

Then at the end you can close the new file:

然后在最后你可以关闭新文件:

csv_out.close

As mentioned in the comments, CSV::Writer is no longer in the standard library. An equivalent version of the code using the newer CSV.foreach (for reading) and CSV.open (for writing) is:

如评论中所述,CSV :: Writer不再位于标准库中。使用较新的CSV.foreach(用于阅读)和CSV.open(用于写入)的代码的等效版本是:

CSV.open("path/to/file.csv", "wb") do |csv_out|
  CSV.foreach("#{RAILS_ROOT}/doc/some.csv") do |row|
    address = row[5]
    l = Location.address_find(address)
    if l != nil
      puts "#{l.name} at #{l.address}"
      row[14] = l.store_code
      puts row[14]
    else
      puts "No matching address Found!!!"
    end
    csv_out << row 
  end
end