0

I get a sort of Error Messagge that I should not use a "bare Except ..." I have to check the 'line of the text file' (saved in raw_results ...) in which the third charachter is a dot ('.'). Is there another way avoiding to use Try ... Except ?

Thx for help

with open(file_to_read, 'r') as fi:
    raw_results = fi.readlines()

date_of_game = []
home_team = []
away_team = []
home_team_goals = []
away_team_goals = []

for i in range(len(raw_results)):
    try:
        if raw_results[i][2] == '.':
            date_of_game.append(raw_results[i][:5])
            if raw_results[i + 1] != "A Tav.":
                home_team.append(raw_results[i + 1].strip('\n'))
                away_team.append(raw_results[i + 2].strip('\n'))
                home_team_goals.append(raw_results[i + 3].strip('\n'))
                away_team_goals.append(raw_results[i + 4].strip('\n'))
            else:
                home_team.append(raw_results[i + 2].strip('\n'))
                away_team.append(raw_results[i + 3].strip('\n'))
                home_team_goals.append(raw_results[i + 4].strip('\n'))
                away_team_goals.append(raw_results[i + 5].strip('\n'))
    except:
        ''' ... do nothing '''
10
  • if the except is doing nothing , why are you adding it , its purpose is debugging and catching exceptions , can you explain your reasoning for adding it ? Commented Jul 6, 2022 at 16:14
  • If I do not put the Except it does not work - I get a SyntaxError: unexpected EOF while parsing Commented Jul 6, 2022 at 16:15
  • yes since you nee dto remove the try also Commented Jul 6, 2022 at 16:17
  • i needed why using the try/except in the first place for your example Commented Jul 6, 2022 at 16:18
  • Without putting a Try/Except I get : if raw_results[i][2] == '.': IndexError: string index out of range Commented Jul 6, 2022 at 16:18

1 Answer 1

1

to solve the bare exception error: you can add

except Exception as error :
   print(error)
   continue

to continue it's looping

Sign up to request clarification or add additional context in comments.

3 Comments

The looping works in every case, but now i get "string index out of range"
i believe it's from raw_results[i + 5] for example if i = len(draw_results)-1 so you' re out of the range of the data
you can correct by limiting your range to range(len(raw_results)-6)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.