question

Steve Holden steve at holdenweb.com
Mon Aug 20 04:52:15 CEST 2007


CarpeSkium at gmail.com wrote:
> On Aug 17, 2:34 pm, Peter Otten <__pete... at web.de> wrote:
>>  CarpeSk... at gmail.com wrote:
>>> "I've parsed a webpage into a text file. In doing so, I've kept all
>>> the text I'm interested in, and removed all the text I don't want. My
>>> result is a text file that iscomma-separated. However, the text file
>>> is one, very long, single string. I need to substitute every eighth
>>> (8th)commawith a new line ("\n"). I've tried regular expressions,
>>> but don't know how to make this happen on only every eighthcomma. I'd
>>> post my code, but none of it works. Thanks for the help."
>>>>> text = "aaa," * 20
>>>>> from itertools import cycle, groupby
>>>>> print "\n".join(",".join(g) for k, g in groupby(text.split(","),
>> ...     key=lambda key, c=cycle([True]*8+[False]*8): c.next()))
>> aaa,aaa,aaa,aaa,aaa,aaa,aaa,aaa
>> aaa,aaa,aaa,aaa,aaa,aaa,aaa,aaa
>> aaa,aaa,aaa,aaa,
>>
>> :-)
>>
>> Peter
> 
> Thanks Peter. I'll try that first thing Monday morning when I'm back
> to work. It
> is a totally different approach than what I was trying with regex.
> 
> And Shawn, I didn't post any of my work because the network I work on
> isn't
> connected to the internet. So it didn't seem constructive to re-type
> all of my
> failed code just to satisfy your "standards" of proving that I've been
> trying to
> hack this myself for the past few days. All in all, thanks for your,
> uhhhh,
> constructive comments.
> 
Aah, right, and the USB thumb drive won't be invented for another 20 
years. Damn.

Sorry, been reading too much bash.org. Damn that Michael Foord ...

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list