[Python] Formattare un file di stringhe in XML

Dante Loi danteloi1993 a gmail.com
Lun 3 Mar 2014 16:19:17 CET


Ho applicato un po dei vostri consigli, tra cui l'usare python3, però non
vedo miglioramenti. Per quanto riguarda la list comprehension, la trovo
molto leggibile e mi piace così com'è!

#!/usr/bin/python3

from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as etree

import sys

infile = open(sys.argv[1], 'r')
outfile = open(sys.argv[2], 'w')

resources = etree.Element("resorurces")
tree = etree.ElementTree(resources)

for line in infile:
    if line.strip(): #if not is an empty line
        name, value = [ word.strip() for word in line.split("=",1) ]
        iteam = etree.Element("string")
        iteam.set("name", name)
        iteam.text = value
        resources.append(iteam)

tree.write(outfile,encoding='utf8')

infile.close()
outfile.close()

Ed'ecco l'errore:

Traceback (most recent call last):
  File "./proTOxml.py", line 23, in <module>
    tree.write(outfile,encoding='utf8')
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 828, in write
    serialize(write, self._root, qnames, namespaces)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 990, in
_serialize_xml
    _serialize_xml(write, e, qnames, None)
  File "/usr/lib/python3.3/xml/etree/ElementTree.py", line 965, in
_serialize_xml
    write("<" + tag)
TypeError: must be str, not bytes


Il giorno 02 marzo 2014 21:24, Marco Beri <marcoberi a gmail.com> ha scritto:

>
> On 2 Mar 2014 19:54, "Dante Loi" <danteloi1993 a gmail.com> wrote:
> >
> > Non mi sono mai posto il problema perche il file che devo formattare ha
> un'uguale in ogni riga, salvo quelle vuote!
>
> Se è una giustificazione, ok, se invece è il motivo, direi che non è valido
>
> :-)
>
> Ciao.
> Marco.
> P.S. Quota per bene, please.
>
> _______________________________________________
> Python mailing list
> Python a lists.python.it
> http://lists.python.it/mailman/listinfo/python
>
>
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20140303/3de9da55/attachment.html>


Maggiori informazioni sulla lista Python