<p>我运行了你的代码,尽我最大的努力减少更改。我不提供新的解决方案,因为我想让你更多地了解代码中的问题,也许对你的学习代码有一些帮助</p>
<pre class="lang-py prettyprint-override"><code>lines=[ '0021 Literacy and numeracy \n',
'\n',
'Literacy and numeracy are programmes or qualifications arranged mainly for adults, designed \n',
'to teach fundamental skills in reading, writing and arithmetic. The typical age range of \n',
'participants can be used to distinguish between detailed field 0011 ‘Basic programmes and \n',
'qualifications’ and this detailed field. \n',
'\n',
'Programmes and qualifications with the following main content are classified here: \n',
'\n',
'0031 Personal skills \n',
'\n',
'Personal skills are defined by reference to the effects on the individual’s capacity (mental, \n',
'social etc.). This detailed field covers personal skills programmes not included in 0011 ‘Basic \n',
'programmes and qualifications’ or 0021 ‘Literacy and numeracy’, giving key competencies and \n',
'transferable skills. \n',
'\n',
'Programmes and qualifications with the following main content are classified here: \n',
'\n']
import re
definition=[]
ocupation=[]
for l,i in enumerate(lines):
#the findall always return match group,if not match the group will be 0
#so make it easy we can use match,and the regex can replace with '\d{4}'
if re.match(r'\d\d\d\d',i) is not None:
ocupation.append(i)
#should init variable before for loop
def1=[]
for j in range(2,10):
#in your case the 6th line is a '\n' too,so I change the test case to double '\n'
if lines[l+j] == '\n' and lines[l+j+2] == '\n':
break
#bypass any '\n'
elif lines[l+j]=='\n':
continue
else:
def1.append(lines[l+j])
definition.append(def1)
</code></pre>