Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get index of line

I'm reading the file, so how do I get the turns, or index for each line?

for line in excelRead:
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]

So, when I read my first line in excel, line variable is equal to line value in excel, but how do I find which turn it is?

like image 488
ipsissimus Avatar asked Oct 16 '25 03:10

ipsissimus


2 Answers

Use enumerate():

for index, line in enumerate(excelRead, start=1):
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]
    print index

The start parameter to the enumerate indicates that you want to start the index at 1, instead of 0.

like image 78
shaktimaan Avatar answered Oct 17 '25 17:10

shaktimaan


Try:

for index, line in enumerate(excelRead, start=1):
    # use index as you wish
    keywrds=[]
    title=line.split("+")
    title=[lines.strip()for lines in title]
    print title[0]

start=1 because by default it starts at 0, but Excel's first row is row 1.

You can find out more about the built-in enumerate function here.

like image 42
s16h Avatar answered Oct 17 '25 17:10

s16h