Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try/Exept prints only ones
#7
I am confused. Your second post with two exception handlers made it look like you are wondering why the exception handler does not always print both print statements. Do you understand now why in your original post the second print statement was skipped if the first print statement raised an error?

This last post sounds like you are wondering why you only see one error message from the log file when there may be many. This is because you only print messages once per log file. The print statements are outside the for loop that scans the log file.

Here is a shorter example I used to scan a file of baseball world series teams. I want to count the number of times the Milwaukee anybodies played in the WS and how many times the Chicago White Sox played in the WS.
import os

t0 = 'Milwaukee'
t1 = 'White Sox'
with open(os.path.join(os.getcwd(), 'debug.txt')) as f: 
    t0list=[]
    t1list=[]     
    for line in f :
        if t0 in line:
            t0list.append(line)
         
        elif t1 in line:
            t1list.append(line)
             
    if t0list:
        print(f'{t0} count -- {len(t0list)} --->>>  {t0list[-1]}')
    if t1list:
        print(f'{t1} count -- {len(t1list)} --->>>  {t1list[-1]}')
Output:
Milwaukee count -- 3 --->>> 1982 St. Louis Cardinals (NL) Milwaukee Brewers (AL) 4–3 White Sox count -- 5 --->>> 2005 Chicago White Sox (AL) Houston Astros (NL) 4–0
Even though Milwaukee has been in the WS 3 times, the program only printed the last time against St Louis in 1982. The same thing happened with the White Sox, only printing their last time, a win of Houston in 2005.

If this is the problem you are having, having multiple errors but only seeing one error printed, the problem is where the print statements are. In the example below I print each match as it is identified.
import os

t0 = 'Milwaukee'
t1 = 'White Sox'
with open(os.path.join(os.getcwd(), 'debug.txt')) as f: 
    t0cnt = 0
    t1cnt = 0    
    for line in f :
        if t0 in line:
            t0cnt+=1
            print(f'{t0cnt} {line.strip()}')
         
        elif t1 in line:
            t1cnt+=1
            print(f'{t1cnt} {line.strip()}')
Output:
1 1906 Chicago White Sox (AL) Chicago Cubs (NL) 4–2 2 1917 Chicago White Sox (AL) New York Giants (NL) 4–2 3 1919 Cincinnati Reds (NL) Chicago White Sox (AL) 5–3 1 1957 Milwaukee Braves (NL) New York Yankees (AL) 4–3 2 1958 New York Yankees (AL) Milwaukee Braves (NL) 4–3 4 1959 Los Angeles Dodgers (NL) Chicago White Sox (AL) 4–2 3 1982 St. Louis Cardinals (NL) Milwaukee Brewers (AL) 4–3 5 2005 Chicago White Sox (AL) Houston Astros (NL) 4–0
I could also save the matches in a list as before and then print out all the items in the list.
import os

t0 = 'Milwaukee'
t1 = 'White Sox'
with open(os.path.join(os.getcwd(), 'debug.txt')) as f: 
    t0list=[]  
    t1list=[]   
    for line in f :
        if t0 in line:
            t0list.append(line)
         
        elif t1 in line:
            t1list.append(line)

    print(f'{t0} World Series Appearances = {len(t0list)}')
    for line in t0list:
        print(f'{line.strip()}')

    print(f'\n{t1} World Series Appearances = {len(t1list)}')
    for line in t1list:
        print(f'{line.strip()}')
Output:
Milwaukee World Series Appearances = 3 1957 Milwaukee Braves (NL) New York Yankees (AL) 4–3 1958 New York Yankees (AL) Milwaukee Braves (NL) 4–3 1982 St. Louis Cardinals (NL) Milwaukee Brewers (AL) 4–3 White Sox World Series Appearances = 5 1906 Chicago White Sox (AL) Chicago Cubs (NL) 4–2 1917 Chicago White Sox (AL) New York Giants (NL) 4–2 1919 Cincinnati Reds (NL) Chicago White Sox (AL) 5–3 1959 Los Angeles Dodgers (NL) Chicago White Sox (AL) 4–2 2005 Chicago White Sox (AL) Houston Astros (NL) 4–0
Either way if you want more than one message printed you need the print statement inside a for loop. For error messages in a log file I would print the messages as they occur and then maybe a count at the end of the file.
Reply


Messages In This Thread
Try/Exept prints only ones - by tester_V - Nov-01-2020, 04:13 AM
RE: Try/Exept prints only ones - by bowlofred - Nov-01-2020, 04:40 AM
RE: Try/Exept prints only ones - by tester_V - Nov-01-2020, 05:52 AM
RE: Try/Exept prints only ones - by tester_V - Nov-01-2020, 06:11 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-01-2020, 10:14 PM
RE: Try/Exept prints only ones - by tester_V - Nov-02-2020, 01:58 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-02-2020, 03:41 AM
RE: Try/Exept prints only ones - by tester_V - Nov-02-2020, 03:44 AM
RE: Try/Exept prints only ones - by tester_V - Nov-02-2020, 03:56 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-02-2020, 04:00 AM
RE: Try/Exept prints only ones - by deanhystad - Nov-02-2020, 04:25 AM
RE: Try/Exept prints only ones - by tester_V - Nov-03-2020, 02:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  zfill prints extra et the end of a var tester_V 4 931 Mar-24-2023, 06:59 PM
Last Post: tester_V
  variable prints without being declared. ClockPillow 2 1,836 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  Output prints Account.id at the end? LastStopDEVS 5 2,845 Dec-19-2020, 05:59 AM
Last Post: buran
  loop only prints last character. mcmxl22 1 1,748 Feb-17-2020, 02:36 AM
Last Post: menator01
  can you understand why this code prints None? arcbal 2 2,787 Mar-13-2019, 02:57 AM
Last Post: arcbal
  What for a file that prints nothing sylas 1 2,196 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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