Форум сайта python.su
Подскажите пожалуйста, каким образом я могу из простейшего *.conf файла вытащить имена секций и значение только первого в секции параметра, чтобы из них создать XML файл.
вот какой xml требуется получить на выходе:
<Main>
<TCMIPPhoneDirectory clearlight=“true” />
<Title>Phonelist</Title>
<Prompt>Prompt</Prompt>
<DirectoryEntry>
<Name>Vasya</Name>
<Telephone>123</Telephone>
</DirectoryEntry>
<DirectoryEntry>
<Name>Ivan</Name>
<Telephone>234</Telephone>
</DirectoryEntry>
</Main>
Заранее спасибо за помощь.
Офлайн
alexvisoПриведи conf-файл.
каким образом я могу из простейшего *.conf файла вытащить имена секций
Офлайн
Спасибо за отзывчивость)
Вот пример моего conf-файла:
[user1] type = peer username = Vasiliy secret = CegthGfhjkm disallow = all allow = alaw,gsm [user2] type = peer secret = wRu8jb0 username = Ivan disallow = all allow = alaw,gsm
import xml.etree.cElementTree as ET import configparser config = configparser.ConfigParser() config.read('sipusers.conf') dictionary = {} for section in config.sections(): dictionary[section] = {} for option in config.options(section): dictionary[section][option] = config.get(section, option) 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 = dictionary[section]['username'] xml = ET.ElementTree(Main) xml.write("phonebook.xml")
Офлайн
alexviso
Вот как бы еще UTF-8 прикрутить, чтобы в xml русские буковки вставали?
xml.write("phonebook.xml", encoding="utf-8", xml_declaration=True)
Отредактировано py.user.next (Сен. 5, 2016 11:57:51)
Офлайн