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