From python-list Fri Aug 17 18:34:14 2007 From: Peter Otten <__peter__ () web ! de> Date: Fri, 17 Aug 2007 18:34:14 +0000 To: python-list Subject: Re: question Message-Id: X-MARC-Message: https://marc.info/?l=python-list&m=118737774609026 CarpeSkium@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 is comma-separated. However, the text file > is one, very long, single string. I need to substitute every eighth > (8th) comma with a new line ("\n"). I've tried regular expressions, > but don't know how to make this happen on only every eighth comma. 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 -- http://mail.python.org/mailman/listinfo/python-list