如何逐行读取 ruby 中的文本文件(在 s3 上托管)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5809093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I read line by line a text file in ruby (hosting it on s3)?
提问by Timothy T.
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.
我有一个记录的文本文件,我想导入到我的 Rails 3 应用程序中。
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?
我该怎么做呢?
回答by fl00r
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
相关话题
回答by Phrogz
You want IO.foreach:
你想要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 CSVlibrary:
或者,如果它确实是制表符分隔的,您可能需要使用CSV库:
File.open('foo.txt') do |f|
CSV.foreach(f, col_sep:"\t") do |csv_row|
# All parsed for you
end
end
回答by Upgradingdave
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
回答by guapolo
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.
您是否尝试过使用OpenURI(http://ruby-doc.org/stdlib-2.1.2/libdoc/open-uri/rdoc/OpenURI.html)?您必须使您的文件可从 S3 访问。
Or try using de aws-sdkgem (http://aws.amazon.com/sdk-for-ruby).
或者尝试使用 de aws-sdkgem ( http://aws.amazon.com/sdk-for-ruby)。
回答by AlexGuti
You can use OpenURIto read remote or local files.
您可以使用OpenURI读取远程或本地文件。
Assuming that your model has an attachment named file:
假设您的模型有一个名为 的附件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

