34

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?

Satchel
  • 15,928
  • 22
  • 103
  • 185

5 Answers5

48
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?

Community
  • 1
  • 1
fl00r
  • 81,403
  • 31
  • 214
  • 233
21

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
Phrogz
  • 284,740
  • 104
  • 634
  • 722
4
  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
Upgradingdave
  • 12,596
  • 10
  • 61
  • 72
1

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).

guapolo
  • 2,303
  • 2
  • 19
  • 19
  • the files would have permission , but I guess it still needs to be able to use the answer at the top using the File class to go through it and I wasn't clear it could. – Satchel Oct 04 '14 at 04:43
1

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
AlexGuti
  • 2,853
  • 1
  • 25
  • 28