[I made it with Python] XML data batch output tool

I wanted to create test data easily, so I created a large amount of test data creation tools in python. Specifically, the file name and file contents are stored in tsv, read this, rewrite only the target tag of the xml template, and output the test xml file in a batch.

Input file preparation

    1. sample.tsv (file name when outputting and array of rewritten part) This is a sample input file.
FILENAME	SENTENCE
sample_1.xml	ice cream
sample_2.xml	chocolate box
sample_3.xml	candy bar
  1. templete.xml (XML that is the source material for output) This is a sample template for output.
<root>
  <Text>sweets</Text> 
  <originalText>sweets</originalText> 
  <head>sweets</head> 
</root>

Source of tool for batch output of XML data

3.exportToXML.py

#!/usr/bin/env python 
import csv 
import xml.etree.ElementTree as ET 
import shutil 
import os 
import glob 
 
 
#Initialize output folder
for file in glob.glob('C:\\tools\\output\\*.xml', recursive=True):
    os.remove(file) 
    
#Get variable declaration file name and rewrite word
#file name
fName = "" 
#And the word of rewriting
fDoc = "" 
 
#Open the input tsv file
tsv_file = open(r"C:\\tools\sample.tsv", "r", encoding="utf-8", errors="", newline="" ) 
#Parse the file with a reader
f = csv.reader(tsv_file, delimiter="\t", doublequote=True, lineterminator="\n", quotechar='"', skipinitialspace=True) 
data = [ v for v in f] 
 
for i in range(len(data)): 
    #Do not read header
        if i > 0: 
            fName = (data[i][0]) 
            fDoc = (data[i][1]) 
            fPath = 'C:\\tools\\output\\' + fName 
            #Copy the template.
            shutil.copy('C:\\tools\\templete.xml',fPath) 
            tree = ET.parse(fPath) 
            root = tree.getroot() 
            for name in root.iter('text'): 
                name.text = fDoc 
                print(name.text)
            for hname in root.iter('head'): 
                hname.text = fName 
            tree.write(fPath, encoding='UTF-8') 
        
     

Execution method

  1. Place the above files (sample.tsv, templete.xml) in C: \ tools and create a ʻoutput` folder under it.   py7.png

Please execute the source of 3 in the python execution environment. The environment creation / execution method is summarized here. ■ Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.6 point

Output result

As shown in the list of tsv files, I was able to change the contents of the template and output the files in a batch.   py8.png

that's all.

Recommended Posts

[I made it with Python] XML data batch output tool
I made blackjack with python!
I made blackjack with Python.
I made wordcloud with Python.
Process Pubmed .xml data with python
I made a daemon with Python
Life game with Python [I made it] (on the terminal & Tkinter)
I made a segment tree with python, so I will introduce it
Process Pubmed .xml data with python [Part 2]
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a tool that makes decompression a little easier with CLI (Python3)
[Python] Python and security-② Port scanning tool made with Python
[Python] I introduced Word2Vec and played with it.
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
GUI image cropping tool made with Python + Tkinter
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I started machine learning with Python Data preprocessing
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
Data analysis with python 2
Data analysis with Python
I made a Twitter BOT with GAE (python) (with a reference)
I made a window for Log output with Tkinter
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
Read json file with Python, format it, and output json
I tried to analyze J League data with Python
I made a repeating text data generation tool "rpttxt"
I made it with processing, "Sakanaction's live Othello guy".
[Python] I made a function that decrypts AES encryption just by throwing it with pycrypto.
Sample data created with python
I tried fp-growth with python
I tried scraping with Python
I want to be able to analyze data with Python (Part 3)
When I made CaboCha usable with python3, I got stuck (Windows 10)
I made a simple typing game with tkinter in Python
I tried to make various "dummy data" with Python faker
I made a package to filter time series with python
Generate XML (RSS) with Python
Try Python output with Haxe 3.2
Get Youtube data with python
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
I made a simple book application with python + Flask ~ Introduction ~
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I made a server with Python socket and ssl and tried to access it from a browser
I made a puzzle game (like) with Tkinter in Python
I made a python text
I tried gRPC with Python