Abhradip
Abhradip

Reputation: 413

Issue regarding parsing a file in ruby on rails

I have a text file (.txt) which contains an array and the elements of these array is in string format but these are also arrays. Just like the following:

For example, lets consider my file as:

["[1, 2016-11-18 07:38:26 +0000, \"ho\"]","[1, 2016-11-18 07:38:29 +0000, \"hose cla\"]", "[1, 2016-11-18 08:24:54 +0000, \"mo\"]"]

Now I want to parse these file and I need to print the data in the following format as:

1   2016-11-18 07:38:26 +0000   ho

1   2016-11-18 07:38:29 +0000   hose

1   2016-11-18 08:24:54 +0000   mo

Now , I just don't know where to start. I know how to parse a text file in ruby but this time its containing an array. So I am confused how to start and did not get the right idea yet.

Please help me by giving me some hints.

Thanks in advance!!!

I am using the following code to store the content of the file into the array. But it is not giving me the proper format. My file contains array in a contiguous manner. There is no new line in it. I think it causing the problem in the following code.

arr=[]

arr=File.foreach('input.txt').map { |line| line.split(' ') }

Upvotes: 1

Views: 149

Answers (3)

Eric Duminil
Eric Duminil

Reputation: 54223

Answer

You could use json to parse your file, which looks like an Array of Strings :

require 'json'
array = JSON.parse(File.read('file.txt')).map{|string| string.delete('[],"')}

Your array now looks like this :

  ["1 2016-11-18 07:38:26 +0000 ho",
   "1 2016-11-18 07:38:29 +0000 hose cla",
   "1 2016-11-18 08:24:54 +0000 mo"]

If you want the first line, just use array.first.

Old answer

This is probably the easiest way, with chomp to remove newlines at the end of each line, and delete to remove all the specified characters :

array = ["[1, 2016-11-18 07:38:26 +0000, \"ho\"]","[1, 2016-11-18 07:38:29 +0000, \"hose cla\"]", "[1, 2016-11-18 08:24:54 +0000, \"mo\"]"]

puts array.map{|string| string.chomp.delete('[],"')}
#=> ["1 2016-11-18 07:38:26 +0000 ho",
#    "1 2016-11-18 07:38:29 +0000 hose cla",
#    "1 2016-11-18 08:24:54 +0000 mo"]

To just show the first line :

puts array.map{|string| string.chomp.delete('[],"')}[0]
#=> "1 2016-11-18 07:38:26 +0000 ho"

Upvotes: 2

Santosh Sharma
Santosh Sharma

Reputation: 2248

Try Following

array = ["[1, 2016-11-18 07:38:26 +0000, \"ho\"]","[1, 2016-11-18 07:38:29 +0000, \"hose cla\"]", "[1, 2016-11-18 08:24:54 +0000, \"mo\"]"]

array.each do |a|
  puts a.gsub(/[\[\]\\",]/, '')
end

Output:

1 2016-11-18 07:38:26 +0000 ho
1 2016-11-18 07:38:29 +0000 hose cla
1 2016-11-18 08:24:54 +0000 mo

To read File

array = File.readlines('path/to/file.txt')

Upvotes: 0

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

arr = ["[1, 2016-11-18 07:38:26 +0000, \"ho\"]","[1, 2016-11-18 07:38:29 +0000, \"hose cla\"]", "[1, 2016-11-18 08:24:54 +0000, \"mo\"]"]

arr.map{ |str| str.gsub(/[\[\]\\",]/, '') }

#=> [
#=>  "1 2016-11-18 07:38:26 +0000 ho",
#=>  "1 2016-11-18 07:38:29 +0000 hose cla",
#=>  "1 2016-11-18 08:24:54 +0000 mo"
#=> ]

Now you can do whatever you want over the data

EDIT:

how to put the content of my file into this array variable?

You can read file with

arr = File.readlines("#{Rails.root}/file.txt").map(&:strip)

strip is just to remove any additional spaces

Upvotes: 1

Related Questions