How to extract tags from HTML file and write them to a new file?

Viewed 186

My HTML file has the format shown below

<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>

I need to extract ALL the content from two tags in the entire HTML file. The content of one of the tags that starts with <unit id ...> is in one line, but the content of the other tag that starts with "<PE producer ..." and ends with '' is spread over different lines. I need to extract the content within these two tags and write the content to a new file one after another. My output should be:

<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>

My code does not extract the content from all the tags of the file. Does anyone have a clue of whats is going on and how I can make this code work properly?

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)
1 Answers

You have a problem with the code where you loop through the matches and write them to file.

If your initid and PE match counts are the same, you may adjust the code to

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) )
Related