0

I have an xml file that I made some changes on and now I would like to export it.

I wrote this code like this:

import xml.etree.ElementTree as ET
import numpy as np
import pandas as pd

# parsing directly.
tree = ET.parse(r'''C:\Users\barbsmor\ArticleData_0200_20210707.xml''')
print('file loaded')
root = tree.getroot()
print(root)
print('root printed')

df = pd.read_excel(r'''C:\Users\data_new_all.xlsx''', engine='openpyxl')

for i in range(1):
    for child in root:
        for child1 in child:  
            for child2 in child1:
                
                if child2.tag == 'LineNumber':
                    print(child2.text)
                    line_number_value = child2.text
                        
                    price_new = df.loc[df['line'] == line_number_value, 'new_price']
                    per_new = df.loc[df['line'] == line_number_value, 'new_per']

                for child3 in child2:

                    if child3.tag == 'GrossPrice':
                        child3.text = price_new
                        
                    for child4 in child3:
                        if child4.tag == 'NumberOfUnitsInPriceBasis':

                            child4.text = per_new                            

print('changes done')
tree.write(r'''C:\Users\test.xml''')

The xml is very nested so I hope all of that works.

But it seems I can't export the data with. How can I export the changed data?

tree.write(r'''C:\Users\test.xml''')

I get this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

mzjn
  • 48,958
  • 13
  • 128
  • 248
HeadOverFeet
  • 768
  • 6
  • 13
  • 33
  • perhaps [this](https://stackoverflow.com/questions/70809477/valueerror-the-truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-i) or [that](https://stackoverflow.com/questions/53830081/python-pandas-the-truth-value-of-a-series-is-ambiguous). are you sure the exception is thrown only at file-write? – Yarin_007 Nov 13 '22 at 19:38
  • Hi, yes I am sure. it printed that changes were done..And then the error was thrown – HeadOverFeet Nov 13 '22 at 19:44
  • alright. did you check out the answers in any case? [this](https://www.learndatasci.com/solutions/python-valueerror-truth-value-series-ambiguous-use-empty-bool-item-any-or-all/) looks great as well – Yarin_007 Nov 13 '22 at 19:47
  • yep, I did and it has to do with dataframe. I think it is due to this ```df.loc[df['line'] == 8706, 'new_price']``` instead of this ```df.loc[df['line'] == 8706, 'new_price'].iloc[0]``` – HeadOverFeet Nov 13 '22 at 20:18

0 Answers0