CSVParser.py 817 B

123456789101112131415161718192021222324252627
  1. import xml.etree.ElementTree as ET
  2. import csv
  3. # def parseCSV:
  4. def parse_csv(file: str):
  5. # build a tree structure
  6. root = ET.Element("mmcommunication")
  7. # parse the csvFile into reader
  8. with open(file) as csvFile:
  9. reader = csv.reader(csvFile, delimiter=",")
  10. # loop through the parsed file, creating packet-elements with the structure of the csvFile as attributes
  11. for line in reader:
  12. packet = ET.SubElement(root, "packet")
  13. for element in line:
  14. element = element.replace(" ", "")
  15. splitel = element.split(":")
  16. key, value = splitel[0], splitel[1]
  17. packet.attrib[key] = value
  18. # writing the ElementTree into the .xml file
  19. tree = ET.ElementTree(root)
  20. tree.write("ExampleCSVBotnetCommunication.xml")
  21. parse_csv("ExampleCSVBotnetCommunication.txt")