Home > OS >  Ruby want to read file line by line and get the particular part out using gsub
Ruby want to read file line by line and get the particular part out using gsub

Time:09-17

I am writing a code to read a text file or csv file line by line which contain url and i want out the id of each url and print or store in text file but when i do i get an error when i use a loop before loop i am able to get it printed line by line. Can any one help me in this. Below is the my code sample.

line_num=0
File.open('/Users/divyanshu/python_imdb/channels/url.txt').each do |line|
video_links = "#{line_num  = 1} #{line}"
# puts video_links

for video_link in video_links 
 
    video_lin = video_link.gsub('https://www.youtube.com/watch?v=', '')
    video_di = video_lin.gsub('?utm_source=komparify&utm_campaign=site&utm_medium=detailpage', '')
    puts video_di
end 

This the error I'm getting

Traceback (most recent call last):
        2: from /Users/divyanshu/python_imdb/url_validation.rb:6:in `<main>'
        1: from /Users/divyanshu/python_imdb/url_validation.rb:6:in `each'
/Users/divyanshu/python_imdb/url_validation.rb:10:in `block in <main>': undefined method `each' for #<String:0x00007fef3d0907c0> (NoMethodError)

and if I run only this part of code its working fine.

line_num=0
File.open('/Users/divyanshu/python_imdb/channels/url.txt').each do |line|
    video_links = "#{line_num  = 1} #{line}"
    puts video_links
end 

CodePudding user response:

Strings Don't Normally #respond_to? :each

The stack trace tells you everything you need to know:

undefined method `each' for #String:0x00007fef3d0907c0 (NoMethodError)

Even assuming that /channels / with a trailing space is a valid portion of the file path, File#open returns a File object rather than a collection of lines. As written, video_links is a String, not a collection such as a Hash or Array, and there's no String#each method. Since for-in loops are syntatic sugar for #each, the object can't respond to the method.

Depending on whether you want to slurp the whole file into an array of lines, or operate linewise, you should use one of the following alternative methods:

  1. File#each_line, inhereted from IO. For example:

    File.open("path/to/file").each_line
    
  2. File#readlines, also inherited from IO. For example:

    File.readlines("path/to/file")
    

CodePudding user response:

Adding on to the answer by @Todd. There is one more issue in your code snippet as I mentioned in my comment. the video_links variable is of class String.

  1. It will contain only the last line of file
  2. You can not iterate over it

For example, a file.txt as below:

one
two
three
four

And code:

File.open('file.txt').each do |line|
  var = line
end

puts var
#=> "four"

So, working with your example, you should define your variable video_links as an (empty) Array and append the lines of file to iterate.

Example:

video_links = []

lines_in_file = File.readlines('/Users/divyanshu/python_imdb/channels/url.txt')

lines_in_file.each do |line|
  video_links.append line
end

video_links.each do |video_link|
  video_lin = video_link.gsub('https://www.youtube.com/watch?v=', '')
  video_di = video_lin.gsub('?utm_source=komparify&utm_campaign=site&utm_medium=detailpage', '')
  puts video_di
end
  • Related