DataType Conversion from number to integer in pandas while writing to XML file

78 Views Asked by At

I'm trying to convert the csv file to the desired output XML file.

Input CSV

Id,SubID,Rank,Size
1,123,1,0.1
1,234,2,0.2
2,123,1,0.1
2,456,2,0.2

Output XML File

<AA_ITEMS>
  <Id ID="1">
    <SubId ID="123.0">
     <Rank>1</Rank>
     <Size>0.1</Size>
    </SubId>
    <SubId ID="234.0">
      <Rank>2</Rank>
      <Size>0.2</Size>
    </SubId>
</Id>
<Id ID="2">
  <SubId ID="456.0">
    <Rank>1</Rank>
    <Size>0.1</Size>
  </SubId>
  <SubId ID="123.0">
    <Rank>2</Rank>
    <Size>0.2</Size>
  </SubId>

My Code Snippet to achieve the above

root = ET.Element('AA_ITEMS')
        for o_id, df_group in df.groupby('ID'):
            coupon_codes = ET.Element('ID', {'ID': str(o_id)})
            for index,row in df_group.iterrows():
                substituted_code = ET.Element('SubID', {'ID': str(row['SubID'])})
                rank_code = ET.Element('RANK')
                rank_code.text = str(row['RANK'])
                pack_size_code = ET.Element('REL_PACK_SIZE')
                pack_size_code.text = str(row['REL_PACK_SIZE'])
                substituted_code.append(rank_code)
                substituted_code.append(pack_size_code)
                coupon_codes.append(substituted_code)
            root.append(coupon_codes)

        min_xml = ET.tostring(root, encoding='utf8')
        with open('/tmp/{}'.format(self.blob_name.split('.')[0]+'.xml'), "wb") as file:
            file.write(min_xml)

However the issue here is the value of the SubID is displayed in the number format("123.0","234.0","456.0") which should be same as that of the input file i.e integer ("123","234","456") without any decimal.

Please advise on how to achieve the above in my code

0

There are 0 best solutions below