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