Portál AbcLinuxu, 5. května 2025 23:16
import config if config.nejaka_boolean_premenna: [nieco urob]
Pro uživatele mi přijde čitelnější a pohodlnější, použít místo XML, Microsoft Windows INI files, modul pro Python ConfigParser, a pěkná knihovna pro C GLib - Key-value file parser.
this
bych to zařadil k povinné četbě.
from xml.dom.minidom import getDOMImplementation, parseString # vytvorim novy xml dokument a naplnim ho daty doc = getDOMImplementation().createDocument(None, 'main-element', None) root = doc.documentElement fel = doc.createElement('first-element') root.appendChild(fel) sel = doc.createElement('second-element') fel.appendChild(sel) text = doc.createTextNode('some text') sel.appendChild(text) # zapisu xml do souboru a hierarchii znazornim tabulatory with open('/home/honzas/temp/myxml.xml', 'w') as xmlfile: doc.writexml(xmlfile, encoding='utf-8', addindent='\t', newl='\n') # pred upravou takto naformatovaneho xml odstranim znaky noveho radku a tabulatory, # jinak by se ve vystupu zdvojily xmlasstring = '' with open('/home/honzas/temp/myxml.xml') as xml: for line in xml: xmlasstring += line.strip() # parsuji string a pridam novy element doc = parseString(xmlasstring) sel = doc.getElementsByTagName('second-element')[0] newelement = doc.createElement('new-element') sel.appendChild(newelement) # zapis do souboru with open('/home/honzas/temp/myxml.xml', 'w') as xmlfile: doc.writexml(xmlfile, encoding='utf-8', addindent='\t', newl='\n')
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.