Im trying to read lines from a text file and print the number of the line and the line next to the number
Ive tried using enumerate but I want to find another way of doing it
the file contains :
pizza hello goodbye
I want to print to the screen
1-pizza 2-hello 3-goodbye
a simple for loop over the file:
lineCounter=0
with open('C:/t.txt') as readObj:
lines=readObj.readlines()
for line in lines:
line=line.rstrip('\n')
lineCounter += 1
print('{}-{}'.format(lineCounter,line))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.