getting attribute of an element with its corresponding Id

2024/10/14 2:25:21

suppose that i have this xml file :

<article-set xmlns:ns0="http://casfwcewf.xsd" format-version="5">
<article><article id="11234"><source><hostname>some hostname for 11234</hostname></source><feed><type weight=0.32>RSS</type></feed><uri>some uri for 11234</uri></article><article id="63563"><source><hostname>some hostname for 63563 </hostname></source><feed><type weight=0.86>RSS</type></feed><uri>some uri  for 63563</uri></article>
.
.
.
</article></article-set>

what I want, is to print each article id with its specific attribute weight in RSS for the whole document (like this).

id=11234 
weight= 0.32id=63563 
weight= 0.86
.
.
.

I used this code to do so,

from lxml import etree
tree = etree.parse("C:\\Users\\Me\\Desktop\\public.xml")for article in tree.iter('article'):article_id = article.attrib.get('id')for weight in tree.xpath("//article[@id={}]/feed/type/@weight".format(article_id)):print(article_id,weight)

and it did not work, could someone help me with this?

Answer

You can do it in two lines if you really want to do so.

>>> from lxml import etree
>>> tree = etree.parse('public.xml')
>>> for item in tree.xpath('.//article[@id]//type[@weight]'):
...     item.xpath('../..')[0].attrib['id'], item.attrib['weight']
... 
('11234', '0.32')
('63563', '0.86')

One xml checker I used insisted on double-quotes around the values for weight. etree croaked on the xml until I dropped the first line in the file; I don't know why.

https://en.xdnf.cn/q/118007.html

Related Q&A

How to install selenium python on Mac

Ive downloaded the Selenium zip file for python and it contains the folder with the setup.py. It says on python.org that I have to type in terminal python setup.py install but it gives me this error th…

aws s3 - object has no attribute server_side_encryption

Can someone please explain the differences in these two calls. The first one gives the correct server_side_encryption and the second one gives an error. The other attributes give the same value-#!/usr/…

Removing nested for loop to find coincidence values

I am currently using a nested for loop to iterate through to arrays to find values that match a certain criterion. The problem is that this method is incredibly inefficient and time consuming. I was to…

Combine two pandas DataFrame into one new

I have two Pandas DataFrames whose data from different sources, but both DataFrames have the same column names. When combined only one column will keep the name.Like this:speed_df = pd.DataFrame.from_d…

Reasons of slowness in numpy.dot() function and how to mitigate them if custom classes are used?

I am profiling a numpy dot product call. numpy.dot(pseudo,pseudo)pseudo is a numpy array of custom objects. Defined as:pseudo = numpy.array([[PseudoBinary(1), PseudoBinary(0), PseudoBinary(1)],[PseudoB…

How to open cmd and run ipconfig in python

I would like to write a script that do something like that: open the cmd and run the commend "ipconfig" and than copy my ip and paste it to a text file. I wrote the beginning of the script …

Using OAuth to authenticate Office 365/Graph users with Django

We are creating an application for use in our organization, but we only want people in our organization to be able to use the app. We had the idea of using Microsofts OAuth endpoint in order to authent…

Python flatten array inside numpy array

I have a pretty stupid question, but for some reason, I just cant figure out what to do. I have a multi-dimensional numpy array, that should have the following shape:(345138, 30, 300)However, it actual…

Peewee and Flask : Database object has no attribute commit_select

Im trying to use Peewee with Flask, but I dont understand why my database connection does not work.config.pyclass Configuration(object): DATABASE = {name: test,engine: peewee.MySQLDatabase,user: root,p…

for loop to create a matrix in python

I am trying to study the probability of having a zero value in my data and I have developed a code that outputs the value of a column of data when the other is zero which is what I need. But having to …