Python Forum
Finding line numbers starting with certain string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding line numbers starting with certain string
#1
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ı)
Reply
#2
you function does not return anything
Reply
#3
(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 ?
Reply
#4
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 517 Dec-15-2020, 05:21 PM
Last Post: snippsat
  How to print string multiple times on new line ace19887 7 1,012 Sep-30-2020, 02:53 PM
Last Post: buran
  Add new line after finding last string in a region Nigel11 1 518 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Searching string in file and save next line dani8586 2 588 Jul-10-2020, 09:03 AM
Last Post: dani8586
  question: finding multiple strings within string djf123 4 854 May-16-2020, 01:00 PM
Last Post: snippsat
  Python convert multi line into single line formatted string karthidec 2 2,921 Dec-23-2019, 12:46 PM
Last Post: karthidec
  line number of first and second occurance of string in a file mdalireza 1 638 Nov-18-2019, 09:55 AM
Last Post: perfringo
  Print string in a single line RavCOder 8 1,133 Nov-08-2019, 09:45 AM
Last Post: perfringo
  Read each line, replace string and save into a new file igormonteiro 2 1,257 Sep-15-2019, 01:24 PM
Last Post: buran
  Finding a specific line in a file Vqlk 3 918 Sep-07-2019, 08:20 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020