Python 3: Finding line in file -


this question possibly duplicate answers find don't seem work. have .txt file full of layout:

artist - song, www.link.com

artist2 - song2, www.link2.com

this general purpose:

uinput = input("input here: ")  save = open("save.txt", "w+") ncount = save.count("\n")  in range(0, ncount):     t = save.readline()     if uinput in t:         print("your string " uinput, " found in" end = "")                 print(t) 

my intention is: if userinput word found in line print entire line or link.

you can use list-comprehension read file , lines contain word, example:

with open('save.txt', 'r') f:     uinput = input("input here: ")     found = [line.rstrip() line in f if uinput.lower() in line.lower()]     if found:         print('found in these lines: ')         print('\n'.join(found))     else:         print('not found.') 

if want print link only, can use:

found = [line.rstrip().split(',')[1] line in f if uinput.lower() in line.lower()] 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -