1
Я использую следующий скрипт для преобразования XML мои продукты в CSVUnicodeEncodeError при написании CSV файл
#!/usr/bin/python
from xml.etree import ElementTree as ET
import csv
tree = ET.parse('ItemCatDesc2.xml')
root = tree.getroot()
columns = ['Name'] + [value.attrib.get('AttributeID') for value in tree.findall('.//Product//Value')]
with open('ItemCatDesc2.csv', 'w') as ofile:
ofile = csv.DictWriter(ofile, set(columns))
ofile.writeheader()
for product in tree.findall('.//Product'):
d = {value.attrib.get('AttributeID') : value.text
for value in product.findall('.//Values/Value')}
d['Name'] = product.findtext('Name')
print d
ofile.writerow(d)
При запуске сценария:
python convert.py ItemCatDesc2.xml > ItemCatDesc2.csv
Я получаю следующее сообщение об ошибке:
Traceback (most recent call last):
File "convert.py", line 21, in <module>
ofile.writerow(d)
File "/usr/lib/python2.7/csv.py", line 152, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in position 362: ordinal not in range(128)
XML-я преобразование является
<?xml version="1.0" encoding="UTF-8" ?>
Да, я уже попробовал, что тот же самый результат. UnicodeEncodeError: кодек ascii не может кодировать символ u '\ xbd' в позиции 362: порядковый номер не в диапазоне (128) – Pop
'# - * - кодирование:' используется только тогда, когда исходный код ** содержит не -ascii, что здесь не так –