views:

97

answers:

3

I have a python list as

 [
     (A,{'a':1,'b':2,'c':3,'d':4}),
     B,{'a':1,'b':2,'c':3,'d':4}),
     ...
     ]

I want to know if there is a standard library of serializing this kind of list to xml or should I hand code it to a file.

Edit : Added Detail

Assuming this is used to construct a message such that

message = A( Feild attributes{'a':1,'b':2,'c':3,'d':4}) || B Field attributes{'a':1,'b':2,'c':3,'d':4}) || C Field attributes{'a':1,'b':2,'c':3,'d':4})

+4  A: 

Does it need to be XML? This is the usual domain of the pickle module.

But, no, there's no standard serialize-Python-object-to-XML library. (I have one I wrote a while ago, it's not published, much less "standard".) There are libraries like lxml for converting XML to Python objects and back, and the usual sax, dom, and expat libraries for reading XML.

Mike DeSimone
`pickle` is insecure and limited to use in a single language. There are almost always better serialization options.
Mike Graham
`ElementTree` is really the nicest way to parse and write XML. In particular, for parsing it is a ton nicer than using `sax` or `dom`.
Mike Graham
It is a message model, It needs to be exported to Excel hence the XML usage.
anijhaw
@anijhaw: Given that constraint, I can say there's no standard library. I'm not even sure how your example constitutes a "message model". If I were trying to import to Excel, I would use the `csv` library and then use Excel's CSV import.
Mike DeSimone
@Mike: Security and language independence weren't specified, and the latter would require the input be more general than the Python list-of-tuples-with-dictionaries given. `pickle` also does its job with very little implementation effort, much less than `ElementTree`, which is not a standard module. That's why I was wondering why there was any need for XML... and here we are.
Mike DeSimone
@Mike, 1) Security and robustness are good goals in most cases, especially the former. 2) I understand why you recommended not using XML if possible and agree; the contention remains that `ElementTree` is easier to use than the other *XML tools* you provided.
Mike Graham
+1  A: 
  • Why are you using XML? There are often better solutions, like JSON, which is plenty portable and standard.
  • The easiest way might be to use YAML. YAML's main representation is not XML, but there is a canonical way (YAXML) to represent YAML serialized data as XML.
Mike Graham
+2  A: 

"use json/yaml/whitespace" comments aside (I suppose you have your reasons to do so, instead go for pickle/json),

you can try the very pythonic elementtree library (in the standardlib), or even use some advice from google : search "converting python dictionary to xml"

(not to sound too rude .. take it with a wink)

looking at your example, what are A and B ? integers ? strings ? classmethods ?

makapuf
:) I did check out the elementtree, This list gives out meta data from a class that describes a messageso A, B , C are fields of the message and the dictonary gives some values specific to the fields
anijhaw