no modifications in XML file after running python code - python

I wrote a code that must modify some values in a xml file. it looks to be working, but when i open this xml file threw PyCharm where i have added the modified file, it just doesn't change a thing. If anyone gave a respond to such a question, please point me where is it. Here is the code as well as the xml.
import xml.etree.ElementTree as ET
tree = ET.parse("farms.xml")
root = tree.getroot()
for elem in root.findall('farm'):
elem.set('money', '2000')
money = elem.get('money')
print(money)
xml
<farms>
<farm farmId="1" name="Моя ферма" color="1" loan="0.000000" money="213" loanAnnualInterestRate="304.166656">
<players>
</players>
</farm>
</farms>

What you are missing is writing the tree back to disk.
import xml.etree.ElementTree as ET
tree = ET.parse("farms.xml")
root = tree.getroot()
for elem in root.findall('farm'):
elem.set('money', '2000')
with open('new_farms.xml', 'wb') as f:
tree.write(f)

It works for me.
Additionally,
print(xml.etree.ElementTree.tostring(root))
will show what you expect.

Related

xml.etree.ElementTree how can i add attribute inside of a node?

I want the build an xml file and i made some research. I decided the use xml tree but i couldn't manage the use it like i want.
I want the generate this xml.
<Invoice test="how can i generate this ?">
</Invoice>
i am doing in python
import xml.etree.ElementTree as gfg
def GenerateXML(fileName):
root = gfg.Element("Invoice")
root.tail = 'test="how can i generate this ?"'
tree = gfg.ElementTree(root)
with open(fileName, "wb") as files:
tree.write(files)
It's generate xml file look like:
<Invoice />test="how can i generate this ?"
I know i shouln't use tail for i want. But i can't find a way for the make a xml look like what i want. Thank you for help.
This piece of XML structure is called "attribute".
You can get it using the set(attr, value) method.
import xml.etree.ElementTree as gfg
def GenerateXML(fileName):
root = gfg.Element("Invoice")
root.set('test', 'how can i generate this ?')
tree = gfg.ElementTree(root)
with open(fileName, "wb") as files:
tree.write(files)
GenerateXML('test.xml')
test.xml:
<Invoice test="how can i generate this ?" />

I can't read an XML file

It's my first time working with XML files, yet I have a problem with a code as simple as:
from xml.etree import ElementTree as ET
tree = ET.parse('some_xml_file.xml')
s = ET.tostring(tree, method = 'xml')
root = tree.getroot()
all I am trying to do here is reading the XML file as a string,
but whenever I try to run this I get an error:
AttributeError: 'ElementTree' object has no attribute 'tag'
I have no idea what I did wrong just yet, so I would need any hint
and thanks in advance
You can't use ET.tostring on the full tree; you can use it on the root element.
from xml.etree import ElementTree as ET
tree = ET.parse('some_xml_file.xml')
s = ET.tostring(tree.getroot(), method='xml')

How to write Element Tree dump into file

I am try to write the xml dump into the another file. Here is my python code
import xml.etree.ElementTree as ET
tree = ET.parse('extract_orginal.xml')
root = tree.getroot()
with open('extract.xml', 'w') as extract:
for item in root.findall(f"doc[#id='289e1292134534']"):
extract.write(ET.dump(item))
Getting the output as "NONE" in the extract.xml file. Can you please help me.
From the docs of .dump():
"Write element tree or element structure to sys.stdout. This function should be used for debugging only."
The function .dump() returns None!
I think you want to use .tostring():
import xml.etree.ElementTree as ET
tree = ET.parse('extract_orginal.xml')
root = tree.getroot()
with open('extract.xml', 'w') as extract:
for item in root.findall(f"doc[#id='289e1292134534']"):
extract.write(ET.tostring(item, encoding="utf-8"))

Append XML responses in Python

I am trying to parse multiple XML responses in one file. However, when I write a responses to file, it shows only last one. I assume I need to add append somewhere in order to keep all responses.
Here is my code:
import json
import xml.etree.ElementTree as ET
#loop test
feins = ['800228936', '451957238']
for i in feins:
rr = requests.get('https://pdb-services.nipr.com/pdb-xml-reports/hitlist_xml.cgi?report_type=0&id_fein={}'.format(i),auth=('test', 'test'))
root = ET.fromstring(rr.text)
tree = ET.ElementTree(root)
tree.write("file.xml")
Try changing
for i in feins:
...
tree = ET.ElementTree(root)
tree.write("file.xml")
to (note the indentation):
for i in feins:
...
tree = ET.ElementTree(root)
with open("file.xml", "wb") as f:
tree.write(f)
and see if it works.

Read an xml file in Python

I am reading a file with a jml extension. The code is very simple and it reads
import xml.etree.ElementTree as ET
tree = ET.parse('VOAPoints_2010_M25.jml')
root = tree.getroot()
but I get a parsing error:
ParseError: not well-formed (invalid token): line 75, column 16
the file I am trying to read is a dataset that has been used before so I am confident that there are no problems with it.
The file is
Can anyone help ?
Sorry for using an answer as a question, but formatting this inside a comment is painful.
Does the code below solve your problem?
import xml.etree.ElementTree as ET
myParser = ET.XMLParser(encoding="utf-8")
tree = ET.parse('VOAPoints_2010_M25.jml',parser=myParser)
root = tree.getroot()
Since the pound sign was the issue, you can escape it with the character entity £. Python can even automate the replace in XML file by iteratively reading each line and replacing it conditionally on the pound symbol:
import xml.etree.ElementTree as ET
oldfile = "VOAPoints_2010_M25.jml"
newfile = "VOAPoints_2010_M25_new.jml"
with open(oldfile, 'r') as otxt:
for rline in otxt:
if "£" in rline:
rline = rline.replace("£", "£")
with open(newfile, 'a') as ntxt:
ntxt.write(rline)
tree = ET.parse(newfile)
root = tree.getroot()

Categories