join , split question

metal metal29a at gmail.com
Thu Nov 5 19:46:04 EST 2009


On 11月6日, 上午8时34分, metal <metal... at gmail.com> wrote:
> On 11月4日, 下午5时39分, elca <high... at gmail.com> wrote:
>
> > Hello,
> > i have some text file list such like following format.
> > i want to change text format to other format.
> >  i was upload it pastebin sitehttp://elca.pastebin.com/d71261168
>
> > if anyone help ,much appreciate thanks in advance
> > --
> > View this message in context:http://old.nabble.com/join-%2C-split-question-tp26193334p26193334.html
> > Sent from the Python - python-list mailing list archive at Nabble.com.
>
> s = """uji708
> uhodih
> utus29
> agamu4
> azi340
> ekon62
> """
>
> from itertools import cycle
> for i, x in zip(cycle(range(3)), s.splitlines()):
>     print x + ',',
>     if i == 2:
>         print
>
> """
> uji708, uhodih, utus29,
> agamu4, azi340, ekon62,
> """

yet another version, a little evil

s = """uji708
uhodih
utus29
agamu4
azi340
ekon62
"""

from itertools import *
print '\n'.join(','.join(x for i, x in g) for k, g in groupby(enumerate
(s.splitlines()), lambda (i, x): i/3))
"""
uji708,uhodih,utus29
agamu4,azi340,ekon62
"""



More information about the Python-list mailing list