如何在Ruby中将一列的内容分为csv的两列?

编程入门 行业动态 更新时间:2024-10-25 06:33:40
本文介绍了如何在Ruby中将一列的内容分为csv的两列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个CSV对象,内容如下:

I got an object of CSV, the content is like:

full_name | phone_number | Mike Smith | (123)-456-7890| Tony Davis | (213)-564-7890|

我想将"full_name"列分为两列,分别代表"first_name"和"last_name":

And I would like to split the column of 'full_name' into two columns, which represents 'first_name' and 'last_name':

first_name | last_name | phone_number | Mike | Smith | (123)-456-7890| Tony | Davis | (213)-564-7890|

我没有找到执行该命令的命令,如何拆分它?非常感谢你!

I didn't find a command to do it, how can I split it? Thank you very much!

此部分的代码为:

def to_csv(options = {}) CSV.generate(options) do |csv| columns = %w[ full_name phone_number ] csv << columns each do |books| csv << columns.map { |column| books.public_send(column) } .map { |column| ActionController::Base.helpers.strip_tags(column.to_s) } end end end

此方法的调用者:

send_data(books.to_csv(col_sep: ','),filename: 'booksAuthorsInfo.csv',type: 'application/csv')

"books"的类类型为ActiveRecord_AssociationRelation,我不知道如何将full_name列拆分为两列,这似乎对String.split没有帮助.

The class type of 'books' is ActiveRecord_AssociationRelation, and I don't know how to split that full_name column into two columns, it looks like the String.split is not helpful.

推荐答案

到目前为止,这个问题还没有足够的道理,因此我希望它最终将被关闭.这是有关如何拆分字段的一些信息,以便我们可以找到问题的真正根源.

So far the question doesn't make enough sense so I expect it'll be closed eventually. Here's some information about how to split the fields so that, maybe, we can get to the real root of the problem.

对此进行冥想:

ary = <<EOT full_name | phone_number | Mike Smith | (123)-456-7890| Tony Davis | (213)-564-7890| EOT PIPE_DELIMITER = /\s*\|\s*/ munged_data = ary.lines[1..-1].map { |l| full_name, phone_number = l.split(PIPE_DELIMITER) # => ["Mike Smith", "(123)-456-7890"], ["Tony Davis", "(213)-564-7890"] first_name, last_name = full_name.split # => ["Mike", "Smith"], ["Tony", "Davis"] [first_name, last_name, phone_number] } # => [["Mike", "Smith", "(123)-456-7890"], ["Tony", "Davis", "(213)-564-7890"]]

此时,可以生成一些可用的CSV数据:

At this point it's possible to generate some usable CSV data:

require 'csv' CSV.open("/dev/stdout", "wb") do |csv| csv << %w[first_name last_name phone_number] munged_data.each do |row| csv << row end end

这将导致:

# >> first_name,last_name,phone_number # >> Mike,Smith,(123)-456-7890 # >> Tony,Davis,(213)-564-7890

请注意使用正则表达式作为split的参数;这告诉split通过仅分割由|分隔的零个或多个空格来清除输出结果:

Note the use of a regular expression as a parameter to split; This tells split to clean up the resulting output a bit by only splitting on zero-or-more whitespace delimited by |:

PIPE_DELIMITER = /\s*\|\s*/ full_name, phone_number = l.split('|') # => ["Mike Smith ", " (123)-456-7890", "\n"], ["Tony Davis ", " (213)-564-7890", "\n"] full_name, phone_number = l.split(PIPE_DELIMITER) # => ["Mike Smith", "(123)-456-7890"], ["Tony Davis", "(213)-564-7890"]

此时,很容易拆分名称:

At that point it is easy to split the names:

first_name, last_name = full_name.split # => ["Mike", "Smith"], ["Tony", "Davis"]

更多推荐

如何在Ruby中将一列的内容分为csv的两列?

本文发布于:2023-10-27 03:34:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   内容   如何在   csv   Ruby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!