Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handling a .txt text
#1
Hi, Folks! I'm trying to open a .txt file and find a line with a specific expression but the return isn't what I expected:

 f = open("file.txt", "r")

 for line in f: 
   
       if "Net" in line: 
       
           print(line)
       else:
           print("Didn't find")

 f.close()
So, what i get is a lot of "Didn't find" but I know that there's this expression in the file. And, I noticed that if i ask for just one caracter, like "N" instead the "Net", the code returns lines with it.
Reply
#2
What do you get if you take out lines 8 and 9? Are you sure 'Net' is in the line with that capitalization?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
    if "Net" in line or 'net' in line:
Is it because you need to look for "net" with a lowercase 'n' too?
Reply
#4
Try this to see if helps,

f = open("file.txt", "r")

for line in f: 
	#print(line.split(' '))  #splitting the line to list before compare
	for item in line.split(' '): 
		if "NET" in item.upper():
			print(line)
		else:
			pass
 
f.close()
Output:
file.txt: sun moon net net a b c def ghi n ij kl mno pqr sty ury mui n pqr stu net wxy python 9jan.py sun moon net net a b c stu net wxy
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#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
#6
Thank you guys for the help!
I'm moving forward with your tips, but it breaks me another issue.

f = open("file.txt", "r")

for line in f:

    for item in line.split(' '):

        if "NET" in item.upper():

            print(line)

        else:
            pass

f.close()
When I open a .txt with more data the return of this code is the error below at PyCharm:

Error:
File "C:\Users\Lzr\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 4748: character maps to <undefined>
I'm wondering there is something wrong in the .txt file but there will be a way to treat this "decode" error.
Thank you again!!

PS.: I liked the way to open the file using 'with'.
Reply
#7
Try:
f = open("file.txt", encoding='utf-8', errors='ignore')
Reply
#8
It works!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,216 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