Answer the question
In order to leave comments, you need to log in
How to print a string with a certain value from a file in python?
Let's say I have a file called 'file.txt'
The lines are written in it:
123456
654321
41444
2423425
255325
How can I find and display the line with the value 654321 if I don't know the number of the line.
Answer the question
In order to leave comments, you need to log in
If you want to display the line 654321 (you wrote exactly that)
But if your task is to "find the line number with the text 654321", then the solution will be different.
And if your task is to read data and write new data instead, then it is wiser to use ready-made solutions - json + dict (for simply storing a small number of infrequently changed key-value pairs) or a database (if there is a lot of data, stability and failure resistance are important)
print("654321")
More or less like this
word=input('Текст:')
i=0
with open('file.txt') as file:
for line in file:
i=i+1
if word in line:
print( line,'Номер строки:', i,end='')
break
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question