获取“错误”除非 - python

发布于 2025-02-13 23:59:51 字数 1102 浏览 0 评论 0原文

我得到了一种错误messagge,我不应该使用“裸露的...” 我必须检查第三个charachter是一个点('。')的“文本文件的行”(保存在RAW_RESULTS ...)中。 有其他方法避免使用尝试...除外吗?

THX寻求帮助

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-02-20 23:59:51

解决裸露的异常错误:
您可以添加

except Exception as error :
   print(error)
   continue

以继续循环

to solve the bare exception error:
you can add

except Exception as error :
   print(error)
   continue

to continue it's looping

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文