Last active
November 13, 2023 16:02
-
-
Save kawishbit/1e1f9cb3dde3bbbba9814f014e0915b6 to your computer and use it in GitHub Desktop.
Convert XML Result to CSV Result for Jmeter using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# 2023 version of https://gist.github.com/tomdottom/b017244a221f8076c4b0adc6feeeb921 | |
# I added 3 new fields (sentBytes, URL, Connect) and used etree instead | |
import csv | |
import sys | |
from pprint import pprint | |
import xml.etree.ElementTree as ET | |
xml_file = 'xml_file.xml' | |
csv_file = 'csv_file.csv' | |
name_map = dict([ | |
('ts', 'timeStamp'), | |
('t', 'elapsed'), | |
('lb', 'label'), | |
('rc', 'responseCode'), | |
('rm', 'responseMessage'), | |
('tn', 'threadName'), | |
('dt', 'dataType'), | |
('s', 'success'), | |
('by', 'bytes'), | |
('ng', 'grpThreads'), | |
('na', 'allThreads'), | |
('lt', 'Latency'), | |
('it', 'IdleTime'), | |
('ct', 'Connect'), | |
('sby', 'sentBytes') | |
]) | |
tree = ET.parse(xml_file) | |
root = tree.getroot() | |
samples = [] | |
for child in root: | |
if(child.tag in ['sample', 'httpSample']): | |
sampleline = {name_map.get(k, k):v for k, v in child.attrib.items()} | |
for subchild in child: | |
if(subchild.tag == 'java.net.URL'): | |
sampleline['URL'] = subchild.text | |
if('URL' not in sampleline): | |
sampleline['URL'] = 'null' | |
samples.append(sampleline) | |
fieldnames = [ | |
'timeStamp','elapsed','label','responseCode', | |
'responseMessage','threadName','dataType','success', | |
'failureMessage','bytes', 'sentBytes', 'grpThreads','allThreads', 'URL', 'Latency','IdleTime', 'Connect'] | |
with open(csv_file, 'w+') as fd: | |
writer = csv.DictWriter(fd, fieldnames) | |
writer.writeheader() | |
writer.writerows(samples) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
# Function to replace content between two words with an empty string | |
def replace_between_words(input_file, output_file, word1, word2): | |
with open(input_file, 'r', encoding='utf-8') as file: | |
content = file.read() | |
modified_content = re.sub(f'{re.escape(word1)}(.*?)\\n*{re.escape(word2)}', f'{word1}{word2}', content, flags=re.DOTALL) | |
with open(output_file, 'w', encoding='utf-8') as output: | |
output.write(modified_content) | |
# Input file name | |
input_file = 'input_xml_file.txt' #change your xml file extension to txt | |
# Output file name | |
output_file = 'output.txt' | |
# Words to define the boundaries | |
word1 = '<responseData class="java.lang.String">' | |
word2 = '</responseData>' | |
# Call the function to replace content between the words | |
replace_between_words(input_file, output_file, word1, word2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment