내 HTML 파일의 형식은 아래와 같습니다.
<unit id="2" status="FINISHED" type="pe">
<S producer="Alice_EN">CHAPTER I Down the Rabbit-Hole</S>
<MT producer="ALICE_GG">CAPÍTULO I Abaixo do buraco de coelho</MT>
<annotations revisions="1">
<annotation r="1">
<PE producer="A1.ALICE_GG"><html>
<head>
</head>
<body>
CAPÍTULO I Descendo pela toca do coelho
</body>
</html></PE>
전체 HTML 파일의 두 태그에서 모든 콘텐츠를 추출해야합니다. <unit id ...>로 시작하는 태그 중 하나의 내용은 한 줄에 있지만 "<PE 생산자 ..."로 시작하고 ''로 끝나는 다른 태그의 내용은 다른 줄에 분산되어 있습니다. . 이 두 태그 내에서 콘텐츠를 추출하고 콘텐츠를 새 파일에 차례로 써야합니다. 내 출력은 다음과 같아야합니다.
<unit id="2" status="FINISHED" type="pe">
<PE producer="A1.ALICE_GG"><html>
<head>
</head>
<body>
CAPÍTULO I Descendo pela toca do coelho
</body>
</html></PE>
내 코드는 파일의 모든 태그에서 콘텐츠를 추출하지 않습니다. 누구든지 무슨 일이 일어나고 있는지 그리고 어떻게이 코드가 제대로 작동하도록 할 수 있는지에 대한 단서를 가지고 있습니까?
import codecs
import re
t=codecs.open('ALICE.per1_replaced.html','r')
t=t.read()
unitid=re.findall('<unit.*?"pe">', t)
PE=re.findall('<PE.*?</PE>', t, re.DOTALL)
for i in unitid:
for j in PE:
a=i + '\n' + j + '\n'
with open('PEtags.txt','w') as fi:
fi.write(a)
일치 항목을 반복하고 파일에 쓰는 코드에 문제가 있습니다.
귀하 initid
와 PE
일치 횟수가 같으면 코드를 다음과 같이 조정할 수 있습니다.
import re
with open('ALICE.per1_replaced.html','r') as t:
contents = t.read()
unitid=re.findall('<unit.*?"pe">', contents)
PE=re.findall('<PE.*?</PE>', contents, re.DOTALL)
with open('PEtags.txt','w') as fi:
for i, p in zip(unitid, PE):
fi.write( "{}\n{}\n".format(i, p) )
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다