Я пытаюсь структурировать данные из текстового файла в файл XML, помечая части текста, которые я хочу пометить тегами XML.
Проблема.
xml.etree.ElementTree не распознает строку
Код пока.
import xml.etree.ElementTree as ET
with open('input/application_EN.txt', 'r') as f:
application_text=f.read()
Первое, что я хочу сделать, это пометить абзацы. текст должен выглядеть так:
<description>
<paragraph id=1>
blabla
</paragraph>
<paragraph id=2>
blabla
</paragraph>
...
</description>
пока я закодировал:
# splitting the text into paragraphs
list_of_paragraphs = application_text.splitlines()
# creating a new list where no_null paragraphs will be added
list_of_paragraphs_no_null=[]
# counter of paragraphs of the XML file
j=0
# Create the XML file with the paragraphs
for i,paragraph in enumerate(list_of_paragraphs):
# Adding only the paragraphs different than ''
if paragraph != '':
j = j + 1
# be careful with the space after and before the tag.
# Adding the XML tags per paragraph
xml_element = '<paragraph id=\"' + str(j) +'\">' + paragraph.strip() + ' </paragraph>'
# Now I pass the whole string to the XML constructor
root = ET.fromstring(description_text)
Я получаю эту ошибку:
неправильно сформирован (неверный токен): строка 1, столбец 6
После некоторого исследования я понял, что ошибка объясняется тем фактом, что в тексте содержится символ «&».
Добавление и удаление «&» в нескольких местах подтверждает это.
Вопрос в том, почему? почему «&» не рассматривается как текст. Что я могу сделать?
Я знаю, что могу заменить все "&", но тогда я потеряю информацию, так как "& Co." Строка довольно важна.
Я бы хотел, чтобы текст остался без изменений. (без изменения содержимого).
Предложения
спасибо.
EDIT:
Чтобы вам было легче, у вас есть начинающий текст, над которым я работаю (вместо того, чтобы открыть файл, вы можете добавить это, чтобы проверить это):
application_text='Language=English
Has all kind of kind of references. also measures.
Photovoltaic solar cells for directly converting radiant energy from the sun into electrical energy are well known. The manufacture of photovoltaic solar cells involves provision of semiconductor substrates in the form of sheets or wafers having a shallow p-n junction adjacent one surface thereof (commonly called the "front surface"). Such substrates may include an insulating anti-reflection ("AR") coating on their front surfaces, and are sometimes referred to as "solar cell wafers". The anti-reflection coating is transparent to solar radiation. In the case of silicon solar cells, the AR coating is often made of silicon nitride or an oxide of silicon or titanium. Such solar cells are manufactured and sold by E.I. duPont de Nemeurs & Co.'
Как видите, в конце есть символ "& Co." что вызывает проблемы.