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'>"WSTR"</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'>"R8"</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.
colName.append(x.find('remote-name').text) if x is not None else Nonewhat is the point of theif ...on that line?xis never in factNone, because if it was, then yourx.findtest would raise anAttributeError.if x.find('precision'):toif x.find('precision') is not None:and see if it works.