By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

List to Text back to List

P: n/a
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
Nov 6 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, Nov 6, 2008 at 12:04 PM, SimonPalmer <si**********@gmail.comwrote:
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/pickl...module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
TIA
Simon
--
http://mail.python.org/mailman/listinfo/python-list
Nov 6 '08 #2

P: n/a
On Nov 6, 8:11*pm, "Chris Rebert" <c...@rebertia.comwrote:
On Thu, Nov 6, 2008 at 12:04 PM, SimonPalmer <simon.pal...@gmail.comwrote:
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/pickl...module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.

Cheers,
Chris
--
Follow the path of the Iguana...http://rebertia.com
TIA
Simon
--
http://mail.python.org/mailman/listinfo/python-list

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.
Nov 6 '08 #3

P: n/a
On Thu, Nov 6, 2008 at 12:18 PM, SimonPalmer <si**********@gmail.comwrote:
On Nov 6, 8:11 pm, "Chris Rebert" <c...@rebertia.comwrote:
>On Thu, Nov 6, 2008 at 12:04 PM, SimonPalmer <simon.pal...@gmail.comwrote:
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/pickl...module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.

Cheers,
Chris
--
Follow the path of the Iguana...http://rebertia.com
TIA
Simon
--
http://mail.python.org/mailman/listinfo/python-list


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
--
Follow the path of the Iguana...
http://rebertia.com
Nov 6 '08 #4

P: n/a
SimonPalmer <si**********@gmail.comwrites:

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.
Simpljson is a package for python 2.3+:

http://pypi.python.org/pypi/simplejson/

--
Arnaud
Nov 6 '08 #5

P: n/a
On Nov 6, 2:18*pm, SimonPalmer <simon.pal...@gmail.comwrote:
On Nov 6, 8:11*pm, "Chris Rebert" <c...@rebertia.comwrote:
On Thu, Nov 6, 2008 at 12:04 PM, SimonPalmer <simon.pal...@gmail.comwrote:
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/pickl...module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.
Cheers,
Chris
--
Follow the path of the Iguana...http://rebertia.com
TIA
Simon
--
>http://mail.python.org/mailman/listinfo/python-list

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.
Nov 6 '08 #6

P: n/a
On Nov 7, 7:04*am, SimonPalmer <simon.pal...@gmail.comwrote:
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
Nov 6 '08 #7

P: n/a
On Nov 6, 8:33*pm, "Chris Rebert" <c...@rebertia.comwrote:
On Thu, Nov 6, 2008 at 12:18 PM, SimonPalmer <simon.pal...@gmail.comwrote:
On Nov 6, 8:11 pm, "Chris Rebert" <c...@rebertia.comwrote:
On Thu, Nov 6, 2008 at 12:04 PM, SimonPalmer <simon.pal...@gmail.comwrote:
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/pickl...module-pickle).
Both modules can convert simple Python data, such as your list of
floats, to a bytestream and back again.
Cheers,
Chris
--
Follow the path of the Iguana...http://rebertia.com
TIA
Simon
--
http://mail.python.org/mailman/listinfo/python-list
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 athttp://www.undefined.org/python/

Cheers,
Chris
--
Follow the path of the Iguana...http://rebertia.com
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.
Nov 7 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.