使用 Ruby 存储 cvs 数据以便进一步操作

发布于 2024-09-02 17:22:05 字数 388 浏览 6 评论 0原文

我正在处理一个 csv 文件,其中包含一些客户信息(电子邮件、姓名、地址、金额、[购物清单:第 1 项、第 2 项])。

我想处理数据并生成一些用于打印的标签...以及收集一些额外的信息(总数、总项目 1...)

我主要关心的是找到适当的结构来将数据存储在 ruby​​ 中以供将来操纵。现在我考虑了以下可能性:

  1. 多维数组:构建起来非常简单,但很难以漂亮的 ruby​​ 方式访问数据。
  2. 哈希:以电子邮件作为密钥,并将信息存储在不同的哈希值(一个哈希值用于名称,另一个哈希值用于地址,另一个哈希值用于购物清单...)
  3. (将 cvs 数据放入数据库并使用 ruby​​ 中的数据?)

我非常感谢您的建议和指导!!

I am dealing with a csv file that has some customer information (email, name, address, amount, [shopping_list: item 1, item 2]).

I would like work with the data and produce some labels for printing... as well as to gather some extra information (total amounts, total items 1...)

My main concern is to find the appropriate structure to store the data in ruby for future manipulation. For now I have thought about the following possibilities:

  1. multidimensional arrays: pretty simple to build, but pretty hard to access the data in a beautiful ruby way.
  2. hashes: having the email as key, and storing the information in different hashes (one hash for name, another hash for address, another hash for shopping list...)
  3. (getting the cvs data in to a Database and working with the data from ruby??)

I would really appreciate your advice and guidance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2024-09-09 17:22:05

一旦您需要将多条信息组合在一起,就可以考虑从通用哈希/数组转向更专业的信息了。您所描述的内容的一个很好的候选者是 Ruby 的 struct 模块

Customer = Struct.new(:email, :name, :address) # etc.

bill = Customer.new('[email protected]', 'Bill Foo', '123 Bar St.')

puts "#{bill.name} lives at #{bill.address} and can be reached at #{bill.email}"

输出:

Bill Foo lives at 123 Bar St. and can be reached at [email protected]

Struct#new 只是为传入的每个符号创建一个带有 attr_accessor 的类。嗯,它实际上创建的内容比这多一点,但是对于初学者来说,这就是您需要担心的。

一旦您将每一行的数据打包到某种类型的对象中(无论是 struct 还是您自己的类),那么您就可以担心如何存储那些对象。

  • 哈希非常适合通过给定键(可能是客户的姓名或其他唯一 ID)进行随机访问,
  • 一维数组可以很好地按照插入的顺序迭代整个客户集

Once you have more than a couple pieces of information that you need to group together, it's time to consider moving from a generic hash/array to something more specialized. A good candidate for what you've described is Ruby's struct module:

Customer = Struct.new(:email, :name, :address) # etc.

bill = Customer.new('[email protected]', 'Bill Foo', '123 Bar St.')

puts "#{bill.name} lives at #{bill.address} and can be reached at #{bill.email}"

Output:

Bill Foo lives at 123 Bar St. and can be reached at [email protected]

Struct#new simply creates a class with an attr_accessor for each symbol you pass in. Well, it actually creates a bit more than that, but for starters, that's all you need to worry about.

Once you've got the data from each row packed into an object of some sort (whether it's a struct or a class of your own), then you can worry about how to store those objects.

  • A hash will be ideal for random access by a given key (perhaps the customer's name or other unique ID)
  • A one-dimensional array works fine for iterating over the entire set of customers in the same order they were inserted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文