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
#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


Messages In This Thread
RE: Finding line numbers starting with certain string - by Yoriz - Jun-27-2020, 12:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pulling Specifics Words/Numbers from String bigpapa 2 903 May-01-2023, 07:22 PM
Last Post: bigpapa
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,801 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 13,022 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,630 Sep-27-2022, 01:38 PM
Last Post: buran
  Finding First Digits in String giddyhead 4 1,517 Aug-17-2022, 08:12 PM
Last Post: giddyhead
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,331 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Find and Replace numbers in String giddyhead 2 1,393 Jul-17-2022, 06:22 PM
Last Post: giddyhead
Question Finding string in list item jesse68 8 2,115 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  append a string to a modified line Mr_Blue 10 4,171 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue
  How to capture string from a line to certain line jerald 1 2,003 Jun-30-2021, 05:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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