fork download
  1. import xml.etree.ElementTree as ET
  2. import pandas as pd
  3.  
  4. xml_file = "exchanger.xml"
  5. output_file = "all_devices_changes.xlsx"
  6.  
  7. try:
  8. tree = ET.parse(xml_file)
  9. root = tree.getroot()
  10. print("Парсинг прошел успешно")
  11. except Exception as e:
  12. print(f"Ошибка: {e}")
  13. exit()
  14.  
  15. all_devices = root.findall('.//Device')
  16. data_rows = []
  17. all_conflicts = []
  18.  
  19. for device in all_devices:
  20. device_name = device.get("Name", "Unknown")
  21. all_states = device.findall('.//State')
  22.  
  23. for state in all_states:
  24. cur_date = state.get("Key", "No_Date")
  25. seen_in_state = {}
  26.  
  27. for prop in state.findall('.//Property'):
  28. key = prop.get("Key")
  29. val = str(prop.get("Value", ""))
  30.  
  31. # Проверка на дубликаты внутри одного стейта
  32. if key in seen_in_state:
  33. all_conflicts.append({
  34. "Device": device_name,
  35. "Date": cur_date,
  36. "Property": key,
  37. "Values": f"{seen_in_state[key]} -> {val}"
  38. })
  39. seen_in_state[key] = val
  40.  
  41. # Собираем общую базу данных
  42. data_rows.append({
  43. "Device": device_name,
  44. "Property": key,
  45. "Date": cur_date,
  46. "Value": val
  47. })
  48.  
  49. if not data_rows:
  50. print("Данные не найдены.")
  51. exit()
  52.  
  53. # Создаем один общий DataFrame
  54. df = pd.DataFrame(data_rows)
  55.  
  56. # Формируем таблицу: строки сгруппированы по Девайсу и Параметру
  57. pivot_df = df.pivot_table(
  58. index=['Device', 'Property'],
  59. columns='Date',
  60. values='Value',
  61. aggfunc=lambda x: " | ".join(x)
  62. )
  63.  
  64. # Сортируем даты (столбцы)
  65. pivot_df = pivot_df.reindex(columns=sorted(pivot_df.columns))
  66.  
  67. # Фильтруем: оставляем только те строки, где значения менялись
  68. # nunique(axis=1) считает уникальные значения в строке (горизонтально)
  69. changes_only = pivot_df[pivot_df.nunique(axis=1, dropna=True) > 1]
  70.  
  71. # Сохраняем в один лист
  72. with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
  73. changes_only.to_excel(writer, sheet_name="Changes_Report")
  74.  
  75. if all_conflicts:
  76. pd.DataFrame(all_conflicts).to_excel(writer, sheet_name="Conflicts_Log", index=False)
  77. print(f"Внимание: обнаружено дублей: {len(all_conflicts)}. См. лист Conflicts_Log")
  78.  
  79. print(f"Файл сохранен: {output_file}")
  80.  
Success #stdin #stdout 2.6s 76804KB
stdin
Standard input is empty
stdout
Ошибка: [Errno 2] No such file or directory: 'exchanger.xml'