1

I am stuck since few days in converting of a xml to csv but i see it going nowhere. I've tried this script with and without special characters and its working perfectly when there is no special special character. Special character, iam refering to german characters ü,ß and all those.

import xml.etree.ElementTree as ET
import csv


tree = ET.parse("demoxml.xml")
root = tree.getroot()

f = open('demoxml.csv', 'w', newline='')

csvwriter = csv.writer(f)

count = 0

head = ['Author','Title','Genre','Price','Publish date', 'description']

csvwriter.writerow(head)

for time in root.findall('book'):
    row = []
    Author = time.find('author').text
    row.append(Author)
    Title = time.find('title').text
    row.append(Title)
    Genre = time.find('genre').text
    row.append(Genre)
    Price = time.find('price').text
    row.append(Price)
    Publish = time.find('publish_date').text
    row.append(Publish)
    Description = time.find('description').text
    row.append(Description)
    csvwriter.writerow(row)
f.close()

demo.xml

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Matthew Müller</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Kim Großer</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>

Where should i put this decode() or encode('utf-8'), i tried it almost at every step but still failed.

1 Answer 1

1

Try it this way, and see if it works:

Replace

import csv

with

import unicodecsv as csv

and

f = open('demoxml.csv', 'w', newline='')
csvwriter = csv.writer(f)

with

f = open('demoxml.csv', 'wb')
csvwriter = csv.writer(f, dialect='excel', encoding='utf-8')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.