我有一个文件,其中第一行是运动类别,随后几行是用分号分隔的值,其中每个值都遵循相同的结构:str-str-int-int。
您将如何读取和处理这些文件,如果结构不同,则会引发错误?我想检查字符串列表的长度以及类型
样本输入:
BASKETBALL
player 1; Peter; 23; 15;
player 2; Steve; 14; 10;
示例错误:
FOOTBALL
player 4; Carl; ten; 22; 15;
谢谢
答案 0 :(得分:2)
对于特定的数据模式,可以使用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))