我该如何解决:ValueError无效的int()底数为10的文本” Python

问题描述 投票:1回答:1
if not os.path.exists(txt_file):
    with open(txt_file, 'w') as f:
        f.write('Score: {score}\n'
                + 'Name: {name}')

with open(txt_file, 'r') as f:
    previous_score = f.readline()

# compare the previous score with current score and write the highest score
if int(previous_score) < int(score):
    with open(txt_file, 'w') as f:
        f.write('Score: {score}\n'
                + 'Name: {name}')

我知道,因为我在线上有Score:,所以它不只是分数而返回。如果使用re.findall,我只能得到分数,但它会变成一个列表,但我仍然不能使用int(previous_score) < int(score)。我该如何进行这项工作?

谢谢上一位评论员Sachin,他帮助我做到了这一点。

python regex python-3.x valueerror
1个回答
0
投票

怎么了previous_score = int(f.readline().split()[-1])

© www.soinside.com 2019 - 2024. All rights reserved.