[Python] Formattare un file di stringhe in XML
Salve devo scrivere un piccolo script che formati, dei file di risorse nel formato XML android. input file: File = File New = Nuovo disegno Open = Apri un file Save = Salva SaveName = Salva con nome Export = Esporta Print = Stampa Close = Chiudi output file: string nameFileFile/string string nameNewNuovo disegno/string ... ... ho buttato giù un paio di righe: #! /usr/bin/python import string import sys string = infile = open(sys.argv[1], 'r') outfile = open(sys.argv[2], 'w') outfile.write(?xml version=\1.0\ encoding=\utf-8\?\nresources\n) for name, equal, value in infile.read().split(): string = string name= + word1 + + word2 + /string\n outfile.write(string) outfile.write(/resources) infile.close() outfile.close() Il problema e che il valore della stringa, può contenere degli spazzi, quindi non posso semplicemente splitarla. Dovrei estrarre tutta la sottostringa che segue = . Avete qualche consiglio sulle funzioni che potrei usare??? Dante ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Formattare un file di stringhe in XML
On 2014-02-24 12:25, Dante Loi wrote: Salve devo scrivere un piccolo script che formati, dei file di risorse nel formato XML android. input file: File = File New = Nuovo disegno [...] Il problema e che il valore della stringa, può contenere degli spazzi, quindi non posso semplicemente splitarla. Dovrei estrarre tutta la sottostringa che segue = . Avete qualche consiglio sulle funzioni che potrei usare??? Per quello che hai chiesto puoi usare split con il secondo parametro, che dice quanti split vuoi al massimo. Splittando su = ti ritrovi con degli spazi prima e dopo i pezzi, che puoi eliminare con strip(). È più robusto che splittare sugli spazi bianchi, perché se per sbaglio ne metti di consecutivi le cose non ti torneranno. In [1]: s = SaveName = Salva con = nome # funziona anche se c'è un = nel valore In [2]: s.split(=, 1) Out[2]: ['SaveName ', ' Salva con = nome'] In [3]: name, value = [ x.strip() for x in s.split(=, 1) ] In [4]: name, value Out[4]: ('SaveName', 'Salva con = nome') La cosa brutta è come generi l'xml: se le stringhe contengono ' ottieni un file non valido. Dovresti usare qualcosa di più robusto per generare xml, come ElementTree (http://docs.python.org/2/library/xml.etree.elementtree.html). -- Daniele ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Formattare un file di stringhe in XML
.split(maxsplit=2) comunque, se il file di input è un .ini, c'è già il modulo configparser: http://docs.python.org/3.3/library/configparser.html e per generare l'xml, ci sono già diversi moduli che lo fanno correttamente: http://docs.python.org/3.3/library/xml.html -- xmpp: berda...@gmail.com bitmessage: BM-2cTYXfGiSTsnx3righ6aHcJSWe4MV17jDP gpg fingerprint: 3F8D53518012716C4EEF7DF67B498306B3BF75A0 (used just for signing commits) ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Formattare un file di stringhe in XML
On 24/02/2014 20:48, Dante Loi wrote: #!/usr/bin/python import string import sys infile = open(sys.argv[1], 'r') outfile = open(sys.argv[2], 'w') outfile.write("?xml version=\"1.0\" encoding=\"utf-8\"?\nresources\n") for line in infile.readlines(): if line.strip(): name, value = [ word.strip() for word in line.split("=",1) ] outfile.write("\tstring name=\"" + name + "\"" + value + "/string\n") outfile.write("/resources") infile.close() outfile.close() se volete proporre qualche miglioria ben'accetta comunque funziona alla perfezione! Per l'output puoi dare un'occhiata a format: http://docs.python.org/2.7/library/stdtypes.html?highlight=format#str.format Ciao diego ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python