Processing a large string

MRAB python at mrabarnett.plus.com
Fri Aug 12 04:15:58 CEST 2011


On 12/08/2011 03:03, goldtech wrote:
> Hi,
>
> Say I have a very big string with a pattern like:
>
> akakksssk3dhdhdhdbddb3dkdkdkddk3dmdmdmd3dkdkdkdk3asnsn.....
>
> I want to split the sting into separate parts on the "3" and process
> each part separately. I might run into memory limitations if I use
> "split" and get a big array(?)  I wondered if there's a way I could
> read (stream?) the string from start to finish and read what's
> delimited by the "3" into a variable, process the smaller string
> variable then append/build a new string with the processed data?
>
> Would I loop it and read it char by char till a "3"...? Or?
>
You could write a generator like this:

def split(string, sep):
     pos = 0
     try:
         while True:
             next_pos = string.index(sep, pos)
             yield string[pos : next_pos]
             pos = next_pos + 1
     except ValueError:
         yield string[pos : ]

string = "akakksssk3dhdhdhdbddb3dkdkdkddk3dmdmdmd3dkdkdkdk3asnsn..."

for part in split(string, "3"):
     print(part)




More information about the Python-list mailing list