Skip to content
Advertisement

Get NA for empty slots in lxml xpath()

I have a big xml (that one): of which I am providing a sample here:

JavaScript

I now want to pull out all biospecimen and concentration_value and be able to associate them with each other in the end. I am trying to do it like this:

JavaScript

The output csv should look like this:

JavaScript

In reality I also pull out many other things with only a single value per metabolite which is why I prefer this csv format. However, since the some of the concentration_value slots are empty I will just get different numbers of specimen and values, and wont be able to tell which belongs which in the end,..

How can I make it that I get something like an NA value for each missing concentration_value? (Ideally while keeping the general structure of the code and the lxml package since I have to pull out a lot of things for which this is already set up)

Advertisement

Answer

An empty element will return a zero length list. That could be used to show NA instead

JavaScript

Using OP’s code

JavaScript

Result

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement