0

I need to scrape the data from text file and insert it into xml template. This is the code am using..

inFile = open("Coler_Goldwater_Hospital_NY2013.txt", 'r')
outFile = open("coler_health.xml", "w")
buffer = []
for line in inFile:
    if line.startswith("III. Health Needs Identified"):
    #if ("Table 24 – Health Insurance Coverage, Baltimore City") in line:
        buffer = ['']
    elif line.startswith("IV. Community Assets Identified"):
        outFile.write("".join(buffer))
        buffer = []
    elif buffer:
        write
        buffer.append(line)
inFile.close()
outFile.close()

the output is written into xml but I want data to insert into particular tag.

<?xml version="1.0" encoding="utf-8"?>
              
<xml>
<Priority Health Needs>

</Priority Health Needs>

<COMMUNITY ASSESSED>

</COMMUNITY ASSESSED>

</xml>

the text from the above python code should get be directly inserted into

<Priority Health Needs>
    
    </Priority Health Needs>

Anyone, any changes to the code.. A little help over here..

Pavan Chakravarthy
  • 573
  • 4
  • 7
  • 16

1 Answers1

1

I'm not sure I get your question... if you need to add tags before writing to the xml file, I guess you may want to try to replace "".join(buffer) by:

"".join(['<Priority Health Needs>'] + buffer + ['</Priority Health Needs>'])

EDIT: it would help if we knew more precisely what's in your original text document, so we can make sure your parsing is right and can figure exactly what kind of output you expect. There are dedicated libraries which help creating xml files (e.g. Creating a simple XML file using python ) but if the task if very simple it's ok to do without.

Community
  • 1
  • 1
etna
  • 1,083
  • 7
  • 13
  • 1
    Thanks etna.. it works like charm.. you thought around for the solution.. I wanted to insert text into the tags & you created the tags in-between the data.. thank you.. – Pavan Chakravarthy Feb 19 '15 at 08:58