在this question之后,我想在字符串中包含更多级别的层次结构。例如,这是我的字符串:
sometext
somemore text here
some other text
course: course1
some details
TestName: test1
some other details
Id Name marks
____________________________________________________
1 student1 65
2 student2 75
3 MyName 69
4 student4 43
some details
TestName: test3
some other details
Id Name marks
____________________________________________________
1 student1 23
3 MyName 63
4 student4 64
course: course2
some details
TestName: test2
some other details
Id Name marks
____________________________________________________
1 student1 84
2 student3 73
some details
TestName: test5
some other details
Id Name marks
____________________________________________________
1 MyName 84
2 student2 73
course: course4
some details
TestName: test1
some other details
Id Name marks
____________________________________________________
1 student1 58
2 student3 89
some details
TestName: test2
some other details
Id Name marks
____________________________________________________
1 student1 97
3 MyName 60
8 student6 82
我希望了解MyName
的详细信息。类似(course1,test1,69),(course1,test3,63),(course2,test5,84),(course4,test2,60)
或类似输出的输出。
我无法一步完成,因此得出了这个:
import re
eachcourse = re.split(r'course: \w+',string1)
courselist = re.findall(r'course: (\w+)',string1)
li =[]
for i,course in enumerate(courselist):
match = re.findall(r".*?TestName: (\w+)(?:(?!\TestName\b).)*MyName\s+(\d+).*?",eachcourse[i+1],re.DOTALL)
li.append((course,match))
print li
给了我
[('course1', [('test1', '69'), ('test3', '63')]), ('course2', [('test5', '84')]), ('course4', [('test2', '60')])]
有更好更清洁的方式吗?
感谢。
答案 0 :(得分:1)
x=re.findall(r"\bcourse: (\w+)(.*?)(?=(?:\bcourse:|$))",x,flags=re.DOTALL)
print [[i[0]]+re.findall(r"TestName: (\w+)(?:(?!\bTestName\b).)*MyName\s*(\d+)",i[1],flags=re.DOTALL) for i in x]
你可以试试这个。虽然格式不完全相同,但它是可用的。