Найти - Пользователи
Полная версия: Из *.conf в XML, требуется помощь.
Начало » Центр помощи » Из *.conf в XML, требуется помощь.
1
alexviso
Подскажите пожалуйста, каким образом я могу из простейшего *.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>

Заранее спасибо за помощь.
py.user.next
alexviso
каким образом я могу из простейшего *.conf файла вытащить имена секций
Приведи conf-файл.
alexviso
Спасибо за отзывчивость)
Вот пример моего 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")

Прошу строго не судить, т.к. это мой первый опыт в программировании. Суть моей задачи в том, чтобы генерировать телефонную книгу для IP-телефонов. Вот как бы еще UTF-8 прикрутить, чтобы в xml русские буковки вставали?
py.user.next
alexviso
Вот как бы еще UTF-8 прикрутить, чтобы в xml русские буковки вставали?
  
xml.write("phonebook.xml", encoding="utf-8", xml_declaration=True)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB