List to Text back to List

S

SimonPalmer

Hi, I am looking for a way to convert a List of floating point numbers
to and from text. I am embedding it in an XML document and am looking
for a neat way to serialise and de-serialise a list from a text node.
I can easily write something to do it manually but I wondered whether
List had native support to go to and from text.

If not List, are there any other collections/arrays that do this?

TIA
Simon
 
C

Chris Rebert

Hi, I am looking for a way to convert a List of floating point numbers
to and from text. I am embedding it in an XML document and am looking
for a neat way to serialise and de-serialise a list from a text node.
I can easily write something to do it manually but I wondered whether
List had native support to go to and from text.

If not List, are there any other collections/arrays that do this?

It's not really a matter of the collection, but more a matter of
serialization format/library.

If you want the serialized representation to be human-readable, use
JSON (http://docs.python.org/library/json.html#module-json).
If that's not a requirement, use pickle
(http://docs.python.org/library/pickle.html#module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.

Cheers,
Chris
 
S

SimonPalmer

It's not really a matter of the collection, but more a matter of
serialization format/library.

If you want the serialized representation to be human-readable, use
JSON (http://docs.python.org/library/json.html#module-json).
If that's not a requirement, use pickle
(http://docs.python.org/library/pickle.html#module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.

Cheers,
Chris

I looked at pickle, but the problem is that I want my XML to be
readable by languages other than python, so it sort of precludes it.
JSON would be perfect, but I think the module only exists in 2.6, is
that right? Unfortunately I am bound to 2.4.

It is looking more and more like I am going to have to roll my own.
 
C

Chris Rebert

I looked at pickle, but the problem is that I want my XML to be
readable by languages other than python, so it sort of precludes it.
JSON would be perfect, but I think the module only exists in 2.6, is
that right? Unfortunately I am bound to 2.4.

It is looking more and more like I am going to have to roll my own.

The module is available as a third-party library for previous Python
versions at http://www.undefined.org/python/

Cheers,
Chris
 
A

Aaron Brady

I looked at pickle, but the problem is that I want my XML to be
readable by languages other than python, so it sort of precludes it.
JSON would be perfect, but I think the module only exists in 2.6, is
that right?  Unfortunately I am bound to 2.4.

It is looking more and more like I am going to have to roll my own.

There is PyYAML, yet-another-markup-language. Bindings go all the way
back to 2.3.

http://pyyaml.org/wiki/PyYAML

You can always just do ' '.join( alist ) to store, and
astring.split( ) to load.
 
J

John Machin

Hi, I am looking for a way to convert a List of floating point numbers
to and from text.  I am embedding it in an XML document and am looking
for a neat way to serialise and de-serialise a list from a text node.
I can easily write something to do it manually but I wondered whether
List had native support to go to and from text.

If not List, are there any other collections/arrays that do this?

What is "List"? Do you mean "list"?

Don't wonder, read the docs.

If you think you can easily write something to do it manually, do so,
and show us the result -- this might help establish exactly what you
mean.

Here's my attempt at doing manually what I think you mean:

| Python 2.1.3 (#35, Apr 8 2002, 17:47:50) [MSC 32 bit (Intel)] on
win32
| >>> L = [1.2, -3.456, 1.0/3]
| >>> L
| [1.2, -3.456, 0.33333333333333331]
| >>> t = ' '.join(map(repr, L))
| >>> t
| '1.2 -3.456 0.33333333333333331'
| >>> L2 = map(float, t.split())
| >>> L2 == L
| 1
| >>>
|
Key features:
* works on older versions of Python
* no imports required
* use of repr() means no loss of significance
* simple text format can be processed easily/manually in just about
any language

HTH,
John
 
S

SimonPalmer

The module is available as a third-party library for previous Python
versions athttp://www.undefined.org/python/

Cheers,
Chris

simplejson did the trick.

the trouble with a couple of suggestions is that they are only really
any good for 1 dimensional lists, although I confess I didn't make
that requirement clear in my original question

sorry for capitalising list, my mistake, hope you weren't too confused.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top