在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)
的输出或类似的输出。我无法一single而就,因此提出了以下建议:
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')])]
有没有更好更好的方法?
谢谢。
最佳答案
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]
您可以尝试一下。尽管格式不完全相同,但是可以使用。
关于python - Python:正则表达式findall用于子类别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30640171/