How to update/modify a XML file in python? -
I have an XML file that I would like to update after data already.
I thought about opening an XML file in "A" mode. The problem is that after the "root" tag is closed, new data will start dumping.
What should I say that someone is too kind to tell me how to remove a last line? File, and then start dumping data from that point and then close the "root" tag.
Of course I can read the whole file and manipulate some strings, but I do not think this is the best idea ..
Thank you for your time.
Quick and easy way, which you should definitely do not ( See below), I want to read the entire file in the list of stars using readline (). I write it if there is a quick and easy solution that you are looking for.
Just open the file (), then call the readline () method. What you will find is a list of all the stars in the file Now, you can easily add wires to the last element (just add an element to the list before the end). Finally, you can write these files using Listlin ().
An example might help:
my_file = open (filename, "r") lines_of_file = my_file .readlines () lines_of_file.insert (-1, " This line has been added before the last line ") my_file.writelines (lines_of_file)
This should not be the reason because, unless you're doing very fast n 'dirty You should use XML Parse, it is a library that allows you to work intelligently with XML, such as the use of concepts like DOM, trees, and nodes. Doing This is not just the proper way to work with XML, it is also the standard way, making your code more portable and easier for other programmers to understand.
In response to Tim being investigated for this purpose, which I think would be a great idea.
Comments
Post a Comment