List to Text back to List

John Machin sjmachin at lexicon.net
Thu Nov 6 17:18:25 EST 2008


On Nov 7, 7:04 am, SimonPalmer <simon.pal... at gmail.com> wrote:
> 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



More information about the Python-list mailing list