Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: txt2xml.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a text file (.txt) to an XML file (.xml).
- Each line in the text file is a separate element in the XML.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'txt2xml.py'.
- 2. Ensure your text file ('example.txt') is in the same directory as the script.
- 3. Run the script.
- 4. The converted XML file ('txt2xml.xml') will be generated in the same directory.
- Note: Adjust the 'txt_filename' and 'xml_filename' variables in the script as needed.
- """
- import xml.etree.ElementTree as ET
- def txt_to_xml(txt_filename, xml_filename):
- root = ET.Element("root")
- with open(txt_filename, 'r') as txtfile:
- for line in txtfile:
- # Assuming each line in the text file is a separate element
- ET.SubElement(root, "element").text = line.strip()
- tree = ET.ElementTree(root)
- tree.write(xml_filename)
- if __name__ == "__main__":
- # Set the filenames for the text and XML files
- txt_filename = 'example.txt'
- xml_filename = 'txt2xml.xml'
- # Convert the text to an XML file
- txt_to_xml(txt_filename, xml_filename)
- print(f"Converted '{txt_filename}' to '{xml_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement