如何在 ruby​​ 中逐行读取文本文件(将其托管在 s3 上)?

发布于 2024-11-03 13:13:15 字数 151 浏览 1 评论 0原文

我知道我以前已经这样做过,并找到了一组简单的代码,但我不记得或找不到它:(。

我有一个记录文本文件,我想导入到我的 Rails 3 应用程序中。

每一行代表一条记录。可能它可能是属性的制表符分隔,但也可以只使用单个值,

我该如何做到这一点?

I know I've done this before and found a simple set of code, but I cannot remember or find it :(.

I have a text file of records I want to import into my Rails 3 application.

Each line represents a record. Potentially it may be tab delimited for the attributes, but am fine with just a single value as well.

How do I do this?

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

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

发布评论

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

评论(5

○闲身 2024-11-10 13:13:15
File.open("my/file/path", "r").each_line do |line|
  # name: "Angela"    job: "Writer"    ...
  data = line.split(/\t/)
  name, job = data.map{|d| d.split(": ")[1] }.flatten
end

相关主题

在 Ruby 中读取文件的所有常见方法有哪些?

File.open("my/file/path", "r").each_line do |line|
  # name: "Angela"    job: "Writer"    ...
  data = line.split(/\t/)
  name, job = data.map{|d| d.split(": ")[1] }.flatten
end

Related topic

What are all the common ways to read a file in Ruby?

绝不放开 2024-11-10 13:13:15

您想要 IO.foreach

IO.foreach('foo.txt') do |line|
  # process the line of text here
end

或者,如果它确实是制表符分隔的,您可能需要使用 CSV 库:

File.open('foo.txt') do |f|
  CSV.foreach(f, col_sep:"\t") do |csv_row|
    # All parsed for you
  end
end

You want IO.foreach:

IO.foreach('foo.txt') do |line|
  # process the line of text here
end

Alternatively, if it really is tab-delimited, you might want to use the CSV library:

File.open('foo.txt') do |f|
  CSV.foreach(f, col_sep:"\t") do |csv_row|
    # All parsed for you
  end
end
青萝楚歌 2024-11-10 13:13:15
  IO.foreach("input.txt") do |line| 
    out.puts line
    # You might be able to use split or something to get attributes
    atts = line.split
  end
  IO.foreach("input.txt") do |line| 
    out.puts line
    # You might be able to use split or something to get attributes
    atts = line.split
  end
终弃我 2024-11-10 13:13:15

您是否尝试过使用 OpenURI (http://ruby-doc.org/stdlib-2.1.2/libdoc/open-uri/rdoc/OpenURI.html)?您必须使您的文件能够从 S3 访问。

或者尝试使用 de aws-sdk gem (http://aws.amazon .com/sdk-for-ruby)。

Have you tried using OpenURI (http://ruby-doc.org/stdlib-2.1.2/libdoc/open-uri/rdoc/OpenURI.html)? You would have to make your files accessible from S3.

Or try using de aws-sdk gem (http://aws.amazon.com/sdk-for-ruby).

深巷少女 2024-11-10 13:13:15

您可以使用 OpenURI 读取远程或本地文件。

假设您的模型有一个名为 file 的附件:

# If object is stored in amazon S3, access it through url
file_path = record.file.respond_to?(:s3_object) ? record.file.url : record.file.path
open(file_path) do |file|
  file.each_line do |line|
    # In your case, you can split items using tabs
    line.split("\t").each do |item|
      # Process item
    end
  end
end

You can use OpenURI to read remote or local files.

Assuming that your model has an attachment named file:

# If object is stored in amazon S3, access it through url
file_path = record.file.respond_to?(:s3_object) ? record.file.url : record.file.path
open(file_path) do |file|
  file.each_line do |line|
    # In your case, you can split items using tabs
    line.split("\t").each do |item|
      # Process item
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文