2016-09-02 6 views
-3

* .conf [セクション名]と最初のパラメータの値(セクションの名前の後ろ)から、どのようにしてXMLファイルを作成するかを教えてくださいPython?私は非常に単純な設定ファイルを持っていますが、各セクションは複数のオプションです。前もって感謝します。pythonを使ってconfigからXMLを生成する

答えて

0

標準ライブラリには、このようなファイルを読むのに役立つconfigparserと、有効なXMLファイルを作成するのに役立つxml.etree.ElementTreeがあります(変更とビルドに関する節を参照)。

+0

ありがとうございます。今私は醜いが判明したが、それは動作します) –

0
import xml.etree.cElementTree as ET # on Python 3.3+ use xml.etree.ElementTree instead 
import configparser 

config = configparser.ConfigParser() 
config.read('sipusers.conf')  

Main = ET.Element("Main") 
ET.SubElement(Main, "TCMIPPhoneDirectory", clearlight="true") 
ET.SubElement(Main, "Title").text = "Phonelist" 
ET.SubElement(Main, "Prompt").text = "Prompt" 

for section in config.sections(): 
    Child = ET.SubElement(Main, "DirectoryEntry") 
    ET.SubElement(Child, "Name").text = section 
    ET.SubElement(Child, "Telephone").text = config.get(section,'username') 

xml = ET.ElementTree(Main) 
xml.write("phonebook.xml") 
関連する問題