Я анализирую XML-файл только с 3 столбцами.Третий столбец (описательная часть) может иногда (1 на 100 записей) быть длиннее 4000 символов.Если оно длиннее 4000 символов, я хочу создать дополнительное поле в своем словаре.Поэтому для каждых 4000 дополнительных символов создайте новое поле.
#this function does various xml cleanups
def getvalueofnode(node):
if node is None:
return None
else:
soup = BeautifulSoup(node.text)
text = soup.get_text()
text = text.replace("\n", " ")
text = text.replace("\r", " ")
text = text.replace(' +', ' ')
text = text.replace('|', '-')
text = text.replace('_+', '_')
return text
record = []
for node in parsedXML.getroot():
recordline = node.find('DATA_RECORD'), node.find('CASE_KEY'), node.find('DESCRIPTION'), node.find('CASE_NARRATIVE')
recordlineprocessed = {
'casekey': getvalueofnode(recordline[1]),
'description': getvalueofnode(recordline[2]),
'narrative': getvalueofnode(recordline[3])
}
record.append(recordlineprocessed)
df_xml = pd.DataFrame.from_dict(record)
df_xml.to_csv(outfile, sep='\t', index = False)