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 <marcob...@gmail.com> ha scritto: > > On 2 Mar 2014 19:54, "Dante Loi" <danteloi1...@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@lists.python.it > http://lists.python.it/mailman/listinfo/python > >
_______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python