Python Forum
Finding line numbers starting with certain string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Finding line numbers starting with certain string (/thread-27926.html)



Finding line numbers starting with certain string - Sutsro - Jun-27-2020

Hi people,

I am trying to get line numbers depending if the line starts with "T.C." however list is empty and printing the number list gives me nothing.

input receives the text there are no problems with that however if returns nothing even tough text has strings starting with "T.C."


def verial():
    input1 = text1.get("1.0",'end-1c')
   

    satırsayısı=0
    isimsatırlistesi=list()
    for line in input1:
        satırsayısı=satırsayısı+1
        if line.startswith("T.C."):
            #bir satır T.C. ilebaşlıyorsa satırsayısını int cinsinden isim satırlistesine ekliyorum
            isimsatırlistesi.append(int(satırsayısı))
            print(satırsayısı)



RE: Finding line numbers starting with certain string - Yoriz - Jun-27-2020

you function does not return anything


RE: Finding line numbers starting with certain string - Sutsro - Jun-27-2020

(Jun-27-2020, 12:19 PM)Yoriz Wrote: you function does not return anything


İt is supposed to record satırsayısı to the list. What i am missing ?


RE: Finding line numbers starting with certain string - Yoriz - Jun-27-2020

you have not returned anything from the function so it will default to returning None
see the comments in the code below
def verial():
    # input1 = text1.get("1.0",'end-1c') commented out to be hard coded
    input1 = ('T.C. find this',
              'A.B dont find this',
              'A.B dont find this',
              'T.C. find this',
              'T.C. find this',
              'A.B dont find this',
              'T.C. find this')

    # satırsayısı = 0 # not required
    isimsatırlistesi = list()
    # add enumerate to count the line number
    for line_number, line in enumerate(input1, start=1):
        # satırsayısı = satırsayısı+1 # not required
        if line.startswith("T.C."):
            # bir satır T.C. ilebaşlıyorsa satırsayısını int cinsinden isim satırlistesine ekliyorum
            # int not required, already a number, satırsayısı replaced with line_number
            isimsatırlistesi.append(line_number)
            print(line_number)  # satırsayısı replaced with line_number
    return isimsatırlistesi  # need to return a value from the function

print(verial())
Output:
1 4 5 7 [1, 4, 5, 7]