0

I have a big input file. I'm just mentioning a sample below:

      <metadata-record class='column'>
        <remote-name>Customer Name</remote-name>
        <remote-type>130</remote-type>
        <local-name>[Customer Name]</local-name>
        <parent-name>[Custom SQL Query]</parent-name>
        <remote-alias>Customer Name</remote-alias>
        <ordinal>5</ordinal>
        <local-type>string</local-type>
        <aggregation>Count</aggregation>
        <contains-null>true</contains-null>
        **<collation>LEN_RUS_S2_WO</collation>**
        <attributes>
          <attribute datatype='string' name='DebugRemoteType'>&quot;WSTR&quot;</attribute>
        </attributes>
      </metadata-record>

      <metadata-record class='column'>
        <remote-name>Discount</remote-name>
        <remote-type>5</remote-type>
        <local-name>[Discount]</local-name>
        <parent-name>[Custom SQL Query]</parent-name>
        <remote-alias>Discount</remote-alias>
        <ordinal>6</ordinal>
        <local-type>real</local-type>
        <aggregation>Sum</aggregation>
        **<precision>15</precision>**
        <contains-null>true</contains-null>
        <attributes>
          <attribute datatype='string' name='DebugRemoteType'>&quot;R8&quot;</attribute>
        </attributes>
      </metadata-record>

I want to write collation when it is available, else precision tag value. But when I try running this code I'm not able to ever get the latter (even if I'm sure that the corresponding node is present).

This is my code:

import xml.etree.cElementTree as et
import shutil, os
colName  = []
colType  = []
colLname = []
colPname = []
colAlias = []
colOrd   = []
colLtype = []
colagg   = []
colcolla = []
colprec  = []
colcnull = []

xmlTree = et.parse('Input.xml')
xmlRoot = xmlTree.getroot()
for x in xmlRoot.findall('./connection/metadata-records/metadata-record'):
            colName.append(x.find('remote-name').text) 
            colType.append(x.find('remote-type').text) 
            colLname.append(x.find('local-name').text) 
            colPname.append(x.find('parent-name').text) 
            colAlias.append(x.find('remote-alias').text) 
            colOrd.append(x.find('ordinal').text) 
            colLtype.append(x.find('local-type').text) 
            colagg.append(x.find('aggregation').text)
            colcnull.append(x.find('contains-null').text)
            
            if x.find('precision'): 
                colprec.append(x.find('precision').text) 
                print("1")
            else:
                colcolla.append(x.find('collation').text) 
                print("2")

but I get the following output, with Attrubute error:

2
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-2006350475d2> in <module>
     14                 print("1")
     15             else:
---> 16                 colcolla.append(x.find('collation').text)
     17                 print("2")
     18 

AttributeError: 'NoneType' object has no attribute 'text'

My output is same as input, for the first metadata record 'collation' tag is there so it should get printed for the next tag precision is there so that should get printed. My output im expecting is as the sample XML that I have pasted above. Now I'm only getting metadata records with collation tag I'm not getting precision.

4
  • colName.append(x.find('remote-name').text) if x is not None else None what is the point of the if ... on that line? Commented Jul 13, 2020 at 21:09
  • Clearly x is never in fact None, because if it was, then your x.find test would raise an AttributeError. Commented Jul 13, 2020 at 21:16
  • 1
    Try changing if x.find('precision'): to if x.find('precision') is not None: and see if it works. Commented Jul 13, 2020 at 21:22
  • @ Jack Fleeting , Thanks for the help :) Commented Jul 14, 2020 at 13:25

1 Answer 1

3

The find method of ElementTree class

Returns an element instance or None.

The conditional if x.find('precision'): you are testing is evaluated as true if its argument is equal to the boolean True. But even if the precision node is found, the returned value won't be a boolean!

You just have to check that the return value of find is different from None:

if x.find('precision') is not None: 
    colprec.append(x.find('precision').text) 
    print("1")
else:
    colcolla.append(x.find('collation').text) 
    print("2")

Output:

2
1
Sign up to request clarification or add additional context in comments.

6 Comments

No, absolutely not robbed! +1
I have added the code and tried but i got the above error. Can you please have a look once ?? Thanks for your help !!
I have edited my question and posted the error down
Thanks much @RobertoCaboni , I made a mistake in indentation so i got the error , now rectified . Thanks much for your help !!
@Aarush nice news. I was just posting to say that I didn't reproduce the issue. Happy to know that you solved it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.