XML log file printing to pretty [duplicate] - python-3.x

This question already has answers here:
Pretty printing XML in Python
(27 answers)
Closed 1 year ago.
I am trying to format output file using XML structure. Tried xml and lxml but no luck for now.
Currently output file look like:
<root><time>2021-07-28 10:27:49,869 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:49,881 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:51,834 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:51,854 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:52,182 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_request id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest>
<time>2021-07-28 10:27:52,201 qwer123 instanceA 10.10.10.1 aaaaa/111 ABC DEFAULT</time><modification_response id="11d18Dqwerty" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0"><version>v_.0156</version><objectclass>objectID</objectclass><identifier qwerty>123321</identifier><modification operation="delete"><valueObject ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0" ytrew:qw2="url:qwertyu:qwerty:qwer:qw:qwer:0:0:type="8"><objectA>123321</objectA></valueObject></modification></qw2:modifyRequest></root>
Using this code right here:
import lxml.etree as etree
xml_input = etree.parse(r'output\output.txt')
print(etree.tostring(xml_input, pretty_print=True),file = open(r'output\output.txt','a'))
file.close()
How can I improve code for this kind of input? What is done wrong? Thanks everyone for any advice.

not long ago have asked question and found the solution.
I have taken code from How to pretty print XML from the command line?
Where xml.dom.minidom is used.
import xml.dom.minidom
with open(r'output\output.txt', 'r+') as file:
s = file.read()
pretty = xml.dom.minidom.parseString(s).toprettyxml()
with open(r'output\output.txt', 'w') as writer:
writer.write(pretty)
Now output looks like expected.

Related

Class and method print mistake

`
class Dog():
spicies="mammal"
def __init__(self,breed,name):
self.breed =breed
self.name =name
def bark(self,number):
print(f"woof {self.name} is my name and the number is{number}")
mydog= Dog(breed='lab',name="kal",)
print(mydog.breed,mydog.name,mydog.spicies,mydog.bark(1))
`
output: woof kal is my name and the number is1
lab kal mammal None
why it print first and there is none where it was suppose to print what is wrong ?
`
class Dog():
spicies="mammal"
def init(self,breed,name):
self.breed =breed
self.name =name
def bark(self,number):
print(f"woof {self.name} is my name and the number is{number}")
mydog= Dog(breed='lab',name="kal",)
print(mydog.breed,mydog.name,mydog.spicies,mydog.bark(1))
`
> output: woof kal is my name and the number is1
lab kal mammal None
**why it print first and there is none where it was suppose to print what is wrong ?**

Searching strings through log with Python 3.8

I am making some sort of script which would return lines from log file. Having issue with printing out lines which does not suite requested conditions.
Leaving example of log file:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
2021-05-14 05:23:11,140 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueB</identifier><object>Data</object>
2021-05-14 05:23:11,431 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueB</identifier><object>Data</object>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '1234xz987'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '1234xz987'
So basic idea is to get lines with ID_valueA, but also I need all the other data blocks which are connected with ID_valueA. So expected result would be:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
For now I have managed to result:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
**
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
**
^
MISSING
I have used below code:
from file_manager import open_log_file
request_id = re.compile(r"requestID=\"[0-9a-zA-Z:-]+\"")
list_of_input_values = []
list_of_output_values = []
filename = []
id_list= []
case1="typeofRequest1"
case2="typeofResponse1"
case3="typeofRequest2"
case4="typeofResponse2"
case5="typeofRequest3"
case6="typeofResponse3"
status = True
while status == True:
used = input('>>> ')
if used.lower() == 'stop':
break
if used.lower() == 'ID_parameter1':
ID_parameter1 = input('Please input ID_parameter1: ')
list_of_input_values.insert(0,ID_parameter1)
continue
if used.lower() == 'ID_parameter2':
ID_parameter2 = input('Please input ID_parameter2: ')
list_of_input_values.insert(1,ID_parameter2)
continue
if used.lower() == 'ID_parameter3':
ID_parameter3 = input('Please input ID_parameter3: ')
list_of_input_values.insert(2,ID_parameter3)
continue
if used.lower() == '':
continue
if used.lower() == 'open':
filename = open_log_file()
for element in filename:
with open(element) as log:
for line in log:
for val in list_of_input_values:
if val in line:
result = request_id.findall(line)
for i in result:
id_list.append(i)
for element in filename:
with open(element) as log:
for line in log:
for i in id_list:
if i in line:
list_of_output_values.append(line)
for line in log:
if i in line:
list_of_output_values.append(line)
else:
if line.__contains__(case1 or case2 or case3 or case4 or case5 or case6 or case7 or case8):
break
else:
list_of_output_values.append(line)
print(list_of_output_values, file=open(r'output\output.txt', "a"))
from file_manager import open_log_file :
def open_log_file():
import tkinter as tk
from tkinter import filedialog
import fileinput
unused_value = ">>> Unexpected input value: None. "
root = tk.Tk()
root.withdraw()
root.attributes('-topmost', True)
root.update()
root.wm_iconbitmap(r'resources\myicon.ico')
filename = filedialog.askopenfilename(multiple=True)
filename = root.tk.splitlist(filename)
filePath = []
for f in filename:
filePath.append(f)
if filename == ():
print(unused_value)
return filePath
Can you please look around to understand what is wrong. I am expecting current loop to work well, but it seems not working that good. Searching is based on request ID parameter, because not every line contains ID_value specific parameter. Thanks in advice.
Morning guys.
Can not explain why, maybe after taking a look of solution someone would give some decisions.
I have used:
print(list_of_output_values, file=open(r'output\output.txt', "a"))
At the end of code. Which gave me following output:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'
Now scrolling down the code, I have decided to change print() function to write() block for clearer output. and using:
with open(r'output\output.txt', "a") as file:
for i in list_of_output_values:
file.write(i)
And now I am getting:
2021-05-14 04:16:22,164 instanceA 10.0.0.1 <request>requestID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:22,215 instanceA 10.0.0.1 <response>responseID = '1234xz987'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,311 instanceA 10.0.0.1 <request>requestID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:16:23,312 instanceA 10.0.0.1 <response>responseID = 'abcd672'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,620 instanceA 10.0.0.1 <request>requestID = 'r#qwsrdq'<identifier>ID_valueA</identifier><object>Data</object>
2021-05-14 04:17:24,750 instanceA 10.0.0.1 <response>responseID = 'r#qwsrdq'<identifier>ID_valueA</identifier><objectError>Error_Data</objectError>
&QADQ;requestID = &Qot;'r#qwsrdq'/version=.../object=.../...
END OF QUERY <response><info><error></error></info></response>
2021-05-14 06:34:12,266 instanceA - WEB_GUI 10.0.0.1 <request>requestID = '12355557'
<identifier>ID_valueA</identifier>
<object>Data</object>
<object>Data</object>
<object>Data</object>
<value>Data</value>
2021-05-14 06:34:12,315 instanceA - WEB_GUI 10.0.0.1 <response>responseID = '12355557'

error: the following arguments are required: host

Trying to visualize the result of various sensor data in python
I can see it on raspberry pi terminal like a linux command python Sensortag2650.py -n 5 -t 1 -T -A -H -M -B -G -K -L -P 54:6C:0E:52:F8:FC
now i want to see the same result on python shell with same code
def main():
import time
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('host', action='store',help='MAC of BT device')
parser.add_argument('-n', action='store', dest='count', default=0,
type=int, help="Number of times to loop data")
parser.add_argument('-t',action='store',type=float, default=5.0, help='time between polling')
parser.add_argument('-T','--temperature', action="store_true",default=False)
parser.add_argument('-A','--accelerometer', action='store_true',
default=False)
parser.add_argument('-H','--humidity', action='store_true', default=False)
parser.add_argument('-M','--magnetometer', action='store_true',
default=False)
parser.add_argument('-B','--barometer', action='store_true', default=False)
parser.add_argument('-G','--gyroscope', action='store_true', default=False)
parser.add_argument('-K','--keypress', action='store_true', default=False)
parser.add_argument('-L','--light', action='store_true', default=False)
parser.add_argument('-P','--battery', action='store_true', default=False)
parser.add_argument('--all', action='store_true', default=False)
arg = parser.parse_args(sys.argv[1:])
print('Connecting to ' + arg.host)
tag = SensorTag(arg.host)
Output :
Sensortag2650.py [-h] [-n COUNT] [-t T] [-T] [-A] [-H] [-M] [-B] [-G]
[-K] [-L] [-P] [--all]
host
Sensortag2650.py: `error: the following arguments are required: host`

How to extract values inside an XML tag-python 3

Nelow is a sample XML file that i want to parse through and get the value between the year tags(2008)
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
Is there any way to extract the data between the year tags (2008.2011,etc) and print it using python?
Here is the code so far:
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
for year in root.iter('year'):
print(year.attrib)
But when i try that code, nothing prints. Any ideas/suggestions?
It's fairly simple to do it using lxml:
from lxml import etree
tree = etree.parse("country_data.xml")
tree.xpath('//year/text()')
Output:
['2008', '2011', '2011']
You can use BeatifulSoup for this.
from bs4 import BeautifulSoup
years = []
with open('country_data.xml') as fp:
soup = BeautifulSoup(fp, 'lxml')
for country in soup.findAll('country'):
years_data = country.find('year')
years.append(years_data.contents[0])
print('Years: {}'.format(years))
Output:
Years: ['2008', '2011', '2011']

Does __str__ still work in python ver3? How do i convert object to string output?

I'm very new to python and django..When i tried to follow the tutorial on djangoproject.com i am unable to produce my object as a string despite trying different variations of the code including the following. I am using python 3x
Trial 1
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
Trial 2
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
return str(Question)
Trial 3
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
print(Question)
Typing this
Question.objects.all()
Still shows
<QuerySet [<Question: Question object]>
Instead of
<QuerySet [<Question: What's up?>]>
Please help!!!!!

Resources