0

I want to generate a set of XML markups and save them to CSV file.

I have this simple markup

markup = '''<Basic xmlns="http://hl7.org/fhir">
<id value="basic-example-narrative"/> 
<text> 
    <status value="additional"/> 
    <div xmlns="http://www.w3.org/1999/xhtml">
    <h1>Narrative</h1>
    {}
    </div>
</text> 
<code> 
    <text value="Example Narrative Tester"/> 
</code> 
</Basic>'''

I generate a string and format this markup to produce the markup with some data.

def generate_markup(text):
    return markup.strip().format(text)

And before saving to the file the output looks like this

<Basic xmlns="http://hl7.org/fhir">
<id value="basic-example-narrative"/> 
<text> 
    <status value="additional"/> 
    <div xmlns="http://www.w3.org/1999/xhtml">
        <h1>Narrative</h1>
        Risus massa pulvinar...
    </div>
</text> 
<code> 
    <text value="Example Narrative Tester"/> 
</code> 
</Basic>

I write it to CSV file like this, where rows - the list of generate texts with markup - basically I expected a record with a single column where each row contains this markup.

def export_csv(filename, rows):    
    with open(filename, 'w') as csvfile:
        data_writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
        for row in rows:
            data_writer.writerow(row.strip())

But the final result looks like this

<,B,a,s,i,c, ,x,m,l,n,s,=,"""",h,t,t,p,:,/,/,h,l,7,.,o,r,g,/,f,h,i,r,"""",>,"
", , ,<,i,d, ,v,a,l,u,e,=,"""",b,a,s,i,c,-,e,x,a,m,p,l,e,-,n,a,r,r,a,t,i,v,e,"""",/,>, ,"

I want to have a CSV that looks like this

<Basic ... ></Basic>
<Basic ...></Basic>

Basically each line is XML.

What is wrong if it saves the text like this?

lapots
  • 12,553
  • 32
  • 121
  • 242

0 Answers0