Русские символы в xml
Пишу программу которая должна сохранять товары в формате xml. Но появилась проблема, русские буквы отображаются знаками вопроса
<listing lang="ru-RU">
<title>text</title>
<content>content</content>
<category>cati</category>
<categoryid>categoryid</categoryid>
<contactname>contactname</contactname>
<price>price</price>
<currency>currency</currency>
<city_area>city_area</city_area>
<city>city</city>
<region>region</region>
<countryId>countryId</countryId>
<country>country</country>
<custom name="tip">tg</custom>
<custom name="p1">�������</custom>
</listing>
Я думаю что дело в кодировке, как мне добавить кодировку в начале скрипта ?
Вот сам скрипт
from xml.dom import minidom
root = minidom.Document()
xml_root = root.createElement('listings')
root.appendChild(xml_root)
u = 0
while u <10:
categoriesChild = root.createElement('listing')
categoriesChild.setAttribute('lang', f'ru-RU')
xml_root.appendChild(categoriesChild)
categoryChild = root.createElement('title')
textName = root.createTextNode('text')
categoryChild.appendChild(textName)
contentChild = root.createElement('content')
content = root.createTextNode('content')
contentChild.appendChild(content)
catChild = root.createElement('category')
catText = root.createTextNode('cati')
catChild.appendChild(catText)
categoryidChild = root.createElement('categoryid')
categoryidText = root.createTextNode('categoryid')
categoryidChild.appendChild(categoryidText)
contactnameChild = root.createElement('contactname')
contactnameText = root.createTextNode('contactname')
contactnameChild.appendChild(contactnameText)
priceChild = root.createElement('price')
priceText = root.createTextNode('price')
priceChild.appendChild(priceText)
currencyChild = root.createElement('currency')
currencyText = root.createTextNode('currency')
currencyChild.appendChild(currencyText)
city_areaChild = root.createElement('city_area')
city_areaText = root.createTextNode('city_area')
city_areaChild.appendChild(city_areaText)
cityChild = root.createElement('city')
cityText = root.createTextNode('city')
cityChild.appendChild(cityText)
regionChild = root.createElement('region')
regionText = root.createTextNode('region')
regionChild.appendChild(regionText)
countryIdChild = root.createElement('countryId')
countryIdText = root.createTextNode('countryId')
countryIdChild.appendChild(countryIdText)
countryChild = root.createElement('country')
countryText = root.createTextNode('country')
countryChild.appendChild(countryText)
tgChild = root.createElement('custom')
tgChild.setAttribute('name', f'tip')
tgText = root.createTextNode('tg')
tgChild.appendChild(tgText)
artChild = root.createElement('custom')
artChild.setAttribute('name',f'p1')
artText = root.createTextNode('Артикул')
artChild.appendChild(artText)
categoriesChild.appendChild(categoryChild)
categoriesChild.appendChild(contentChild)
categoriesChild.appendChild(catChild)
categoriesChild.appendChild(categoryidChild)
categoriesChild.appendChild(contactnameChild)
categoriesChild.appendChild(priceChild)
categoriesChild.appendChild(currencyChild)
categoriesChild.appendChild(city_areaChild)
categoriesChild.appendChild(cityChild)
categoriesChild.appendChild(regionChild)
categoriesChild.appendChild(countryIdChild)
categoriesChild.appendChild(countryChild)
categoriesChild.appendChild(tgChild)
categoriesChild.appendChild(artChild)
u += 1
xml_root.appendChild(categoriesChild)
xml_str = root.toprettyxml(indent="\t")
save_path_file = "yandex2.xml"
with open(save_path_file, "w") as f:
f.write(xml_str)
Ответы (1 шт):
Автор решения: koolaa12
→ Ссылка
Глупый вопрос, сам же понял что сделать
with open(save_path_file, "w",encoding='utf-8') as f:
просто добавил кодировку при записи файла