+ 1
How do I read one word or one number from the file?
2 Respuestas
+ 4
In Python, you can read a punch of bytes using read(size), or read the entire file using read(). It is also possible to read line by line using readline() or readlines().
To read a word from a file, you can read a line, and split it into words.
In your case, if you want to read the first word in the file, you can use readline once to read first line, and split it into words.
with open("file.txt") as f:
FirstWord = f.readline().split()[0]
if you want to read a specific word from a certain line, you can use readlines() and specify the line you want.
# To read the 3rd word in the 6th line.
LineNumber = 5
WordNumber = 2
with open("file.txt") as f:
Word = f.readlines()[LineNumber].split()[WordNumber]
0
Thank you for your elaborated answer