这是解析文本文件并将它们转储到变量中的代码。下面的代码说明了如何使用正则表达式解析您的文本文件。
import re
fp = open('class_data.txt')
lines = fp.read(-1)
fp.close()
records = re.split('\n\s*\n', lines) #Split all the records
#print len(records)
for record in records:
data = record.split('\n')
classid, classtime, top_students = data[0], data[1], re.split('^[A-Za-z ]*:', data[2])[1].split(',')
attendance, score, completed = re.split('^[A-Za-z ]*:', data[4])[1], re.split('^[A-Za-z ]*:', data[5])[1], data[6]
print classid, classtime, top_students, len(top_students), attendance, score, completed
打印语句输出
13A 11:00 [' Joe Smith', ' Tom Clarke', ' Jenna Sole'] 3 98.5% 54 Yes
13B 11:10 [' Anni Moy', ' Jessica Longate', ' Phillip Tome'] 3 98.5% 54 Yes
14A 11:10 [' John Doe', ' John Smith', ' Sam Ben'] 3 98.5% 54 Yes
现在您已将文本文件转换为变量,我们现在可以添加代码来检查类是否已完成以及记录是否已包含在文件中,否则添加它
import re
fp = open('class_data.txt')
lines = fp.read(-1)
fp.close()
completed = Yes
class = 13A
time = 11:00
isClassRecordFound = False
records = re.split('\n\s*\n', lines) #Split all the records
#print len(records)
for record in records:
data = record.split('\n')
classid, classtime, top_students = data[0], data[1], re.split('^[A-Za-z ]*:', data[2])[1].split(',')
attendance, score, completed = re.split('^[A-Za-z ]*:', data[4])[1], re.split('^[A-Za-z ]*:', data[5])[1], data[6]
print classid, classtime, top_students, len(top_students), attendance, score, completed
if (completed):
if (classid == class) and (time == classtime):
isClassRecordFound = True
break;
if not isClassRecordFound:
with open("class_data.txt", "a") as myfile:
myfile.write(class + '\n' + time)