我有一个文件,其中第一行是运动类别,随后几行是用分号分隔的值,其中每个值都遵循相同的结构:str-str-int-int。

您将如何读取和处理这些文件,并在结构不同的情况下引发错误?我想检查字符串列表的长度以及类型

输入样例:

BASKETBALL
player 1; Peter; 23; 15;
player 2; Steve; 14; 10;


样本错误:

FOOTBALL
player 4; Carl; ten; 22; 15;


谢谢

最佳答案

对于特定的数据模式,可以使用Regular Expression来检查数据的结构:

import re

# Here is the specific pattern you want to match
regex = re.compile(r'^player \d+; [A-Z][a-z]+; \d+; \d+;$')
filename = 'sport.txt'
# Test for each file
with open(filename) as f:
    header = f.readline().strip()
    for line in f:
        if not regex.match(line):
            # You can also catch this Error and do some other things
            raise ValueError('Line Not Match: ' + line)

print('Sports {} in {} is GOOD!'.format(header, filename))

08-28 01:13
查看更多