/
s-s-a
/
xml2csv
Обзор
Документация
Войти
/
s-s-a
/
xml2csv
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
xml2csv.py
36 строк
2 KB
s-s-a
update xml2csv.py
19 апр 2024, 15:31
19 апр 2024, 15:31
65eaf8c
Код
Авторство
О чём код?
# noinspection PyCompatibility import csv import os from xml.dom.minidom import parse aFiles: list = [x for x in os.listdir() if x.endswith("z.xml")] for xFile in aFiles: with open(xFile[0:8] + '.csv', 'w', encoding='cp1251', errors='replace', newline='') as csvfile: csv_file = csv.writer(csvfile, delimiter=';') DOMTree = parse(xFile) count: int = 0 for sotr in DOMTree.getElementsByTagName('Сотрудник'): count += 1 # print (sotr) LC = FM = IM = OT = SS = '' LC = sotr.getElementsByTagName( 'ЛицевойСчет').item(0).firstChild.nodeValue # type: ignore FM = sotr.getElementsByTagName( 'Фамилия').item(0).firstChild.nodeValue # type: ignore IM = sotr.getElementsByTagName('Имя').item(0).firstChild.nodeValue # type: ignore try: OT = sotr.getElementsByTagName( 'Отчество').item(0).firstChild.nodeValue # type: ignore except AttributeError: pass # Игнорируем пустое отчество у иностранцев # replace'ами удаляем последствия повышения "человекочитаемости" SS = sotr.getElementsByTagName('Сумма').item( 0).firstChild.nodeValue.replace('\t', '').replace('\n', '') # type: ignore if len(SS) == 0: SS = sotr.getElementsByTagName( 'Сумма').item(1).firstChild.nodeValue # type: ignore print(LC, FM, IM, OT, SS) csv_file.writerow([LC, FM, IM, OT, SS]) print(f'Записей в файле {xFile}: {count}')