lundi 28 septembre 2020

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

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)

Aucun commentaire:

Enregistrer un commentaire