Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handling a .txt text
#5
I suggest to use different style for opening files (using 'with'). In order to verify processing all rows one can use enumerate and printing out:

with open('for_net.txt', 'r') as f:
    for i, row in enumerate(f, start=1):
        if 'net' in row.lower():
            print(f'Row #{i} contains "net": {row.strip()}')
        else:
            print(f'Row #{i} does not contain "net"')
if 'for_net.txt' contains following four rows:

Quote:This is not
but here is net
and not
and net

Code will output:

Output:
Row #1 does not contain "net" Row #2 contains "net": but here is net Row #3 does not contain "net" Row #4 contains "net": and net
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Handling a .txt text - by reinalazaro - Jan-09-2020, 01:27 AM
RE: Handling a .txt text - by ichabod801 - Jan-09-2020, 03:18 AM
RE: Handling a .txt text - by Clunk_Head - Jan-09-2020, 03:24 AM
RE: Handling a .txt text - by sandeep_ganga - Jan-09-2020, 03:26 AM
RE: Handling a .txt text - by perfringo - Jan-09-2020, 11:09 AM
RE: Handling a .txt text - by reinalazaro - Jan-11-2020, 01:59 AM
RE: Handling a .txt text - by Clunk_Head - Jan-11-2020, 02:16 AM
RE: Handling a .txt text - by reinalazaro - Jan-11-2020, 09:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,321 Nov-09-2022, 07:29 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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