How can I convert XML into a Python object?
I need to load an XML file and convert the contents into an object-oriented Python structure. I want to take this:
<main>
<object1 attr="name">content</object>
</main>
And turn it into something like this:
main
main.object1 = "content"
main.object1.attr = "name"
The XML data will have a more complicated structure than that and I can't hard code the element names. The attribute names need to be collected when parsing and used as the object properties.
How can I convert XML data into a Python object?
Asked by: Emily142 | Posted: 28-01-2022
Answer 1
It's worth looking at lxml.objectify
.
xml = """<main>
<object1 attr="name">content</object1>
<object1 attr="foo">contenbar</object1>
<test>me</test>
</main>"""
from lxml import objectify
main = objectify.fromstring(xml)
main.object1[0] # content
main.object1[1] # contenbar
main.object1[0].get("attr") # name
main.test # me
Or the other way around to build xml structures:
item = objectify.Element("item")
item.title = "Best of python"
item.price = 17.98
item.price.set("currency", "EUR")
order = objectify.Element("order")
order.append(item)
order.item.quantity = 3
order.price = sum(item.price * item.quantity for item in order.item)
import lxml.etree
print(lxml.etree.tostring(order, pretty_print=True))
Output:
<order>
<item>
<title>Best of python</title>
<price currency="EUR">17.98</price>
<quantity>3</quantity>
</item>
<price>53.94</price>
</order>
Answered by: Luke303 | Posted: 01-03-2022
Answer 2
I've been recommending this more than once today, but try Beautiful Soup (easy_install BeautifulSoup).
from BeautifulSoup import BeautifulSoup
xml = """
<main>
<object attr="name">content</object>
</main>
"""
soup = BeautifulSoup(xml)
# look in the main node for object's with attr=name, optionally look up attrs with regex
my_objects = soup.main.findAll("object", attrs={'attr':'name'})
for my_object in my_objects:
# this will print a list of the contents of the tag
print my_object.contents
# if only text is inside the tag you can use this
# print tag.string
Answered by: Kristian124 | Posted: 01-03-2022
Answer 3
David Mertz's gnosis.xml.objectify would seem to do this for you. Documentation's a bit hard to come by, but there are a few IBM articles on it, including this one (text only version).
from gnosis.xml import objectify
xml = "<root><nodes><node>node 1</node><node>node 2</node></nodes></root>"
root = objectify.make_instance(xml)
print root.nodes.node[0].PCDATA # node 1
print root.nodes.node[1].PCDATA # node 2
Creating xml from objects in this way is a different matter, though.
Answered by: Walter116 | Posted: 01-03-2022Answer 4
How about this
http://evanjones.ca/software/simplexmlparse.html
Answered by: Walter714 | Posted: 01-03-2022Answer 5
#@Stephen:
#"can't hardcode the element names, so I need to collect them
#at parse and use them somehow as the object names."
#I don't think thats possible. Instead you can do this.
#this will help you getting any object with a required name.
import BeautifulSoup
class Coll(object):
"""A class which can hold your Foo clas objects
and retrieve them easily when you want
abstracting the storage and retrieval logic
"""
def __init__(self):
self.foos={}
def add(self, fooobj):
self.foos[fooobj.name]=fooobj
def get(self, name):
return self.foos[name]
class Foo(object):
"""The required class
"""
def __init__(self, name, attr1=None, attr2=None):
self.name=name
self.attr1=attr1
self.attr2=attr2
s="""<main>
<object name="somename">
<attr name="attr1">value1</attr>
<attr name="attr2">value2</attr>
</object>
<object name="someothername">
<attr name="attr1">value3</attr>
<attr name="attr2">value4</attr>
</object>
</main>
"""
#
soup=BeautifulSoup.BeautifulSoup(s)
bars=Coll()
for each in soup.findAll('object'):
bar=Foo(each['name'])
attrs=each.findAll('attr')
for attr in attrs:
setattr(bar, attr['name'], attr.renderContents())
bars.add(bar)
#retrieve objects by name
print bars.get('somename').__dict__
print '\n\n', bars.get('someothername').__dict__
output
{'attr2': 'value2', 'name': u'somename', 'attr1': 'value1'}
{'attr2': 'value4', 'name': u'someothername', 'attr1': 'value3'}
Answered by: Daisy678 | Posted: 01-03-2022
Answer 6
There are three common XML parsers for python: xml.dom.minidom, elementree, and BeautifulSoup.
IMO, BeautifulSoup is by far the best.
http://www.crummy.com/software/BeautifulSoup/
Answered by: Owen461 | Posted: 01-03-2022Answer 7
If googling around for a code-generator doesn't work, you could write your own that uses XML as input and outputs objects in your language of choice.
It's not terribly difficult, however the three step process of Parse XML, Generate Code, Compile/Execute Script does making debugging a bit harder.
Answered by: Elise892 | Posted: 01-03-2022Similar questions
How to convert JSON data into a Python object?
I want to convert JSON data into a Python object.
I receive JSON data objects from the Facebook API, which I want to store in my database.
My current View in Django (Python) (request.POST contains the JSON):
response = request.POST
user = FbApiUser(user_id = response['id'])
user.name = response['name']
user.username = response['username']
user.save()
This wor...
python - convert array to json object
I have a 2d array like so:
main_array -> [object,object,object,....]
each object -> [ var_some_string,parent_id,some_random_int] -> fixed length
I need to convert this array into a json object like this..
{ var_some_string: { var_some_string : { var_some_string: -1}}}
The parent_id is the main_array object location which is the par...
c++ - Convert python object to c array
What is the simple and effective way to create c contiguous array from python object? Suppose I wish to create C++ Matrix class, that can be constructed using python object.
template<typename T>
struct Matrix
{
Matrix(PyObject* obj)
{
// extract nrows, ncols, allocate data and copy content
}
T* data;
int nrows;
int ncols;
};
...
Convert Python object to C void type
How can I convert Python object to C void type using Cython?
Currently I am getting this message when I try to cast
Casting temporary Python object to non-numeric non-Python type
python - Can't convert 'int' object to str
I have the following code that causes the below error.
elif args[0]=="online":
onlines = zxLoLBoT.get_friends_online(self)
self.message(sender, "Toplam "+len(onlines)+" kişi açık.")
python - How can I convert a dict to a JSON object?
I have a dict, ast that is stored as:
{u'databaseConnections': {u'shard1': {u'username': u'user'}}}
I want to convert this into JSON so I can do something like:
user = dbConf['databaseConnections']['shard1']['username']
I've tried using json.dumps(ast, ensure_ascii=False), but this just gives me the error:
print dbConf['databa...
Convert python object to list of lists
I'm trying to convert a sqlalchemy object to a list of lists:
I've tried doing:
lambda q: [location.name for obj in q.all()]
and it worked.
But when I wanted to add/append another one to a list, it breaks:
lambda q: [(location.name, obj.name for obj, location in q.all()]
This is what I wanted it to be:
lambda q: [
[location.name, str(ob...
python - How to convert object type based on type?
I have:
x = float(1.0)
y = int(2)
t1 = type(x)
t2 = type(x).__name__
If I print t1 and t2 I can see the following:
print t1
>>> <type 'float'>
print t2
>>> float
How can I use t1 or t2 to change y into type float with the least amount of code?
Python - Convert JSON object data into a List
I have data in a JSON object with a key:value as shown below in Python. There are two records having same ID 13 for the Hari and 16 for the Liz.
from collections import defaultdict
from itertools import *
from operator import itemgetter
data = [
{
"fname": "Abc",
"ln...
python - convert list of data object to csv
I'm using python 2.7.6. I would like to convert my list of objects into csv format. I have a list of cdr object, this object contains some string, int and datatime object.
class cdr():
def __init__(self):
# some init
def __iter__(self):
return iter(self.name, self.my_value,self.my_datetime)
#from another class
import csv
def make_csv(self, cdr_list):
with open(self.file_name, '...
python - Convert a dict object to nested list
I was looking for how to create nested list from dictionary.
Here is the given Dictionary.
Dict={'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections'],'Jason Bourne':['black berry','mango','potato','oli','key'],'fruits':['cherry','pineapple','banana','coconut']}
Excepted output:
[['Jaso...
python - Cant convert an object into an int
I have a list with values that should be number. Right now they are an object however:
later object
opstarten object
dtype: object
I have tried to change the column to a str type by doing:
df_analyse_num[["later"]] = df_analyse_num[["later"]].astype(str)
This does not seem to work however cause when I analyse my types it still says object.
python - How to convert json to object?
I need to convert a json-string to python object. By object I mean "new" python3 object like:
class MyClass(object):
I found several help for example on jsonpickle documentation. But all I found are tutorials which convert object to json first and after this convert backwards.
I want to convert a json-string from a
python - Can't convert numpy object array to float array
When I load in the saved array from a .npy file, I get an array(data_train) which contains two arrays, which looks like the one posted down below.
When I read the docs correct it is a 2d array with two arrays in it, isn t it?
So my problem is, that I do not know how to "extract" the first array(img) out of the one loaded(data_train) in from the .npy file.
python - Convert object to another class
I've searched similar questions about converting classes but none have really helped me.
I have a Fraction class and want to simplify it to an int if possible.
class Fraction:
def __init__(self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
self.simplify()
if self.denominator == 1:
# convert self to int of value nu...
python - Check object type and convert to list
I have two methods:
async def broadcast(self, message: WebSocketMessage):
for web_socket in self._websockets:
await web_socket.send(json.dumps(message.as_dict()))
async def broadcast_all(self, messages: List[WebSocketMessage]):
dumped_messages = json.dumps([msg.as_dict() for msg in messages])
for web_socket in self._websockets:
await web_socket.send(dumped_messages)
python - convert PIL Image object to File object
Is there any way (without saving a file to disk and then deleting it) to convert a PIL Image object to a File object?
How to convert Python object to C++ type in Cython
How can I convert a Python object argument in a Cython method defined using def to a C++ type? I am attempting to provide a Cython wrapper class for a C++ library, as described in the Using C++ in Cython section of the Cython documentation.
Here is an example...
python - Convert numpy object array to sparse matrix
I would like to convert a numpy array with dtype=object to a sparse array e.g. csr_matrix. However, this fails.
x = np.array(['a', 'b', 'c'], dtype=object)
csr_matrix(x) # This fails
csc_matrix(x) # This fails
Both of the calls to sparse matrices produce the following error:
TypeError: no supported conversion for types: (dtype('O'),)
python - Jjpeg image numpy object convert to pygam
This question already has answers here:
python - How to convert local time string to UTC?
How do I convert a datetime string in local time to a string in UTC time?
I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future.
Clarification: For example, if I have 2008-09-17 14:02:00 in my local timezone (+10), I'd like to generate a string with the equivalent UTC time:
python - using jython and open office 2.4 to convert docs to pdf
I completed a python script using pyuno which successfully converted a document/ xls / rtf etc to a pdf.
Then I needed to update a mssql database, due to open office currently supporting python 2.3, it's ancientness, lacks support for decent database libs. So I have resorted to using Jython, this way im not burdened down by running inside OO python environment using an old pyuno.
This also means that my conversion c...
python - Convert a string with date and time to a date
This question already has answers here:
How to convert XML to JSON in Python?
This question already has answers here:
python - How to convert a string of bytes into an int?
How can I convert a string of bytes into an int in python?
Say like this: 'y\xcc\xa6\xbb'
I came up with a clever/stupid way of doing it:
sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))
I know there has to be something builtin or in the standard library that does this more simply...
This is different from
python - Convert number to binary string
Is this the best way to convert a Python number to a hex string?
number = 123456789
hex(number)[2:-1].decode('hex')
Sometimes it doesn't work and complains about Odd-length string when you do 1234567890.
Clarification:
I am going from int to hex.
Also, I need it to be escaped.
IE:
1234567890 -> '\x49\x96\x02\xd2' not '499602D2'
Also, it needs to be ...
python - Convert list of ints to one number?
I have a list of integers that I would like to convert to one number like:
numList = [1, 2, 3]
num = magic(numList)
print num, type(num)
>>> 123, <type 'int'>
What is the best way to implement the magic function?
EDIT
I did find this, but it seem...
How to convert XML to JSON in Python
This question already has answers here:
php - Convert param into python?
I am trying to learn web programming in python. I am converting my old php-flash project into python. Now, I am confused about how to set param value and create object using python.
FYI I used a single php file, index.php to communicate with flash.swf. So, my other php files like login.php, logout.php, mail.php, xml.php etc used to be called from this.
Below is the flash object call from index.php-
xml - How to convert XSD to Python Class
I just want to know if there is a program that can convert an XSD file to a Python class as JAXB does for Java?
python - How to convert local time string to UTC?
How do I convert a datetime string in local time to a string in UTC time?
I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future.
Clarification: For example, if I have 2008-09-17 14:02:00 in my local timezone (+10), I'd like to generate a string with the equivalent UTC time:
How do I convert a list of ascii values to a string in python?
I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?
python - using jython and open office 2.4 to convert docs to pdf
I completed a python script using pyuno which successfully converted a document/ xls / rtf etc to a pdf.
Then I needed to update a mssql database, due to open office currently supporting python 2.3, it's ancientness, lacks support for decent database libs. So I have resorted to using Jython, this way im not burdened down by running inside OO python environment using an old pyuno.
This also means that my conversion c...
python - Convert a string with date and time to a date
This question already has answers here:
How to convert XML to JSON in Python?
This question already has answers here:
How do I convert part of a python tuple (byte array) into an integer
I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:
response = (0, 0, 117, 143, 6)
The first 4 values are a 32-bit int telling me how many bytes have been used and the last value is the percentage used.
I can access the tuple as response[0] but cannot see how I can get the firs...
python - How to convert a string of bytes into an int?
How can I convert a string of bytes into an int in python?
Say like this: 'y\xcc\xa6\xbb'
I came up with a clever/stupid way of doing it:
sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))
I know there has to be something builtin or in the standard library that does this more simply...
This is different from
python - Convert number to binary string
Is this the best way to convert a Python number to a hex string?
number = 123456789
hex(number)[2:-1].decode('hex')
Sometimes it doesn't work and complains about Odd-length string when you do 1234567890.
Clarification:
I am going from int to hex.
Also, I need it to be escaped.
IE:
1234567890 -> '\x49\x96\x02\xd2' not '499602D2'
Also, it needs to be ...
How do I convert a string to a double in Python?
I would like to know how to convert a string containing digits to a double.
python - Convert list of ints to one number?
I have a list of integers that I would like to convert to one number like:
numList = [1, 2, 3]
num = magic(numList)
print num, type(num)
>>> 123, <type 'int'>
What is the best way to implement the magic function?
EDIT
I did find this, but it seem...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python