list to tuple conversion

S

sc

clp:

Thanx to a recent thread I am able to have a print string
with a variable number of formatters -- what I now lack for
the creation of an elegant print statement is a tuple --
following is the code, the last line of which does not work:

<code>
#!/usr/bin/python
import xml.sax
import eaddyhandler
parser = xml.sax.make_parser()
h = eaddyhandler.EAddyHandler()
parser.setContentHandler(h)
parser.parse(".ea.xml")
for i in range(1, len(h.m)):
k = "r%06d" % i
col = len(h.m[k])
if col > 2 and h.m[k][0] > " ":
print (col * '%-30s') % h.m[k]
</code>

What's going on is I have an oocalc spreadsheet for
e-addresses -- column 1 has the name, and then I keep
adding e-addresses for ppl when they get new ones, as
successive entries on their row, meaning each row has
a variable number of e-address columns. I have an xml
extractor that runs before this script using
odf.opendocument, which works famously.

My class, EAddyHandler, also works, and builds its dictionary
of rows in 'm', forgive me, no flames please, I needed a
short name for the dictionary I have to type it so many times.
The key to 'm' is an 'r' + row number, so I can get
stuff out of it and it's still in the right order, fun
with dictionaries.

What I was hoping for was something that could vary the
source for the print statement as cleanly as the 'col'
multiplication creates the print format, but the list,
'h.m[k]' is not a tuple, it's a list, and I'm just not
quite where I am trying to get with this.

If there were a builtin function that took a list and
returned a tuple, I'd be there, but if there is such a
thing I need someone to point me at it. I can't help
thinking I am missing some obvious construct, and I'll
be advised to go reread the tutorial, but I'm not there,
and if you can take pity on me and point me there, I'll
be your friend for life. Well -- I'll be grateful...

TIA,

Scott
 
P

Pekka Laukkanen

2008/10/1 sc said:
If there were a builtin function that took a list and
returned a tuple, I'd be there, but if there is such a
thing I need someone to point me at it. I can't help
thinking I am missing some obvious construct, and I'll
be advised to go reread the tutorial, but I'm not there,
and if you can take pity on me and point me there, I'll
be your friend for life. Well -- I'll be grateful...
(1, 2, 3)
 
G

Gary M. Josack

sc said:
clp:

Thanx to a recent thread I am able to have a print string
with a variable number of formatters -- what I now lack for
the creation of an elegant print statement is a tuple --
following is the code, the last line of which does not work:

<code>
#!/usr/bin/python
import xml.sax
import eaddyhandler
parser = xml.sax.make_parser()
h = eaddyhandler.EAddyHandler()
parser.setContentHandler(h)
parser.parse(".ea.xml")
for i in range(1, len(h.m)):
k = "r%06d" % i
col = len(h.m[k])
if col > 2 and h.m[k][0] > " ":
print (col * '%-30s') % h.m[k]
</code>

What's going on is I have an oocalc spreadsheet for
e-addresses -- column 1 has the name, and then I keep
adding e-addresses for ppl when they get new ones, as
successive entries on their row, meaning each row has
a variable number of e-address columns. I have an xml
extractor that runs before this script using
odf.opendocument, which works famously.

My class, EAddyHandler, also works, and builds its dictionary
of rows in 'm', forgive me, no flames please, I needed a
short name for the dictionary I have to type it so many times.
The key to 'm' is an 'r' + row number, so I can get
stuff out of it and it's still in the right order, fun
with dictionaries.

What I was hoping for was something that could vary the
source for the print statement as cleanly as the 'col'
multiplication creates the print format, but the list,
'h.m[k]' is not a tuple, it's a list, and I'm just not
quite where I am trying to get with this.

If there were a builtin function that took a list and
returned a tuple, I'd be there, but if there is such a
thing I need someone to point me at it. I can't help
thinking I am missing some obvious construct, and I'll
be advised to go reread the tutorial, but I'm not there,
and if you can take pity on me and point me there, I'll
be your friend for life. Well -- I'll be grateful...

TIA,

Scott

--
http://mail.python.org/mailman/listinfo/python-list
>>> L = [1,2,3,4,5]
>>> t = tuple(L)
>>> t
(1, 2, 3, 4, 5)
 
S

sc

Gary said:
sc said:
clp:

Thanx to a recent thread I am able to have a print string
with a variable number of formatters -- what I now lack for
the creation of an elegant print statement is a tuple --
following is the code, the last line of which does not work:

<code>
#!/usr/bin/python
import xml.sax
import eaddyhandler
parser = xml.sax.make_parser()
h = eaddyhandler.EAddyHandler()
parser.setContentHandler(h)
parser.parse(".ea.xml")
for i in range(1, len(h.m)):
k = "r%06d" % i
col = len(h.m[k])
if col > 2 and h.m[k][0] > " ":
print (col * '%-30s') % h.m[k]
</code>

What's going on is I have an oocalc spreadsheet for
e-addresses -- column 1 has the name, and then I keep
adding e-addresses for ppl when they get new ones, as
successive entries on their row, meaning each row has
a variable number of e-address columns. I have an xml
extractor that runs before this script using
odf.opendocument, which works famously.

My class, EAddyHandler, also works, and builds its dictionary
of rows in 'm', forgive me, no flames please, I needed a
short name for the dictionary I have to type it so many times.
The key to 'm' is an 'r' + row number, so I can get
stuff out of it and it's still in the right order, fun
with dictionaries.

What I was hoping for was something that could vary the
source for the print statement as cleanly as the 'col'
multiplication creates the print format, but the list,
'h.m[k]' is not a tuple, it's a list, and I'm just not
quite where I am trying to get with this.

If there were a builtin function that took a list and
returned a tuple, I'd be there, but if there is such a
thing I need someone to point me at it. I can't help
thinking I am missing some obvious construct, and I'll
be advised to go reread the tutorial, but I'm not there,
and if you can take pity on me and point me there, I'll
be your friend for life. Well -- I'll be grateful...

TIA,

Scott

--
http://mail.python.org/mailman/listinfo/python-list
L = [1,2,3,4,5]
t = tuple(L)
t
(1, 2, 3, 4, 5)

fine, documented now, for the world to see, I'm an idiot,
fine, but anyway, thank you both, I'll shutup now.

sc
 
G

Gabriel Genellina

Thanx to a recent thread I am able to have a print string
with a variable number of formatters -- what I now lack for
the creation of an elegant print statement is a tuple --
following is the code, the last line of which does not work:

for i in range(1, len(h.m)):
k = "r%06d" % i
col = len(h.m[k])
if col > 2 and h.m[k][0] > " ":
print (col * '%-30s') % h.m[k]
</code>

Another alternative would be:

print ''.join('%-30s' % item for item in h.m[k])

You may need to add square brackets [ ] inside the parenthesis () if your
Python version is older than 2.5
 

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
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top