[Tutor] String Processing Query

spawgi at gmail.com spawgi at gmail.com
Tue May 17 10:54:52 CEST 2011


This snipped also works - may be there is a better solution -

', '*']
>>> s = ''
>>> for line in lines:
line = line.rstrip('\n')
 if line.startswith('>from header'):
continue
elif line.startswith('*'):
 s = s + '\n'
else:
s = s + line

>>> s
'abcdefghijklmnopqrstuvwxyz\npoiuytrewqlkjhgfdsamnbvcxz\n'
>>> print s
abcdefghijklmnopqrstuvwxyz
poiuytrewqlkjhgfdsamnbvcxz


Thanks and Regards,
Sumod

On Mon, May 16, 2011 at 7:35 PM, James Reynolds <eire1130 at gmail.com> wrote:

> concatenate the entire thing together, including the "*".
>
> Once you have that as a single string, use string.split('*') and you will
> have your two strings.
>
> On Mon, May 16, 2011 at 9:51 AM, Spyros Charonis <s.charonis at gmail.com>wrote:
>
>> I have a file with the following contents:
>>
>> >from header1
>> abcdefghijkl
>> mnopqrs
>> tuvwxyz
>> *
>> >from header2
>> poiuytrewq
>>  lkjhgfdsa
>> mnbvcxz
>> *
>>
>> My string processing code goes as follows:
>>
>> file1=open('/myfolder/testfile.txt')
>> scan = file1.readlines()
>>
>> string1 = ' '
>> for line in scan:
>>     if line.startswith('>from'):
>>         continue
>>     if line.startswith('*'):
>>         continue
>>     string1.join(line.rstrip('\n'))
>>
>> This code produces the following output:
>>
>> 'abcdefghijkl'
>> 'mnopqrs'
>> 'tuvwxyz'
>> 'poiuytrewq'
>> 'lkjhgfdsa'
>> 'mnbvcxz'
>>
>> I would like to know if there is a way to get the following
>> output instead:
>>
>> 'abcdefghijklmnopqrstuvwxyz'
>>
>> 'poiuytrewqlkjhgfdsamnbvcxz'
>>
>> I'm basically trying to concatenate the strings
>> in order to produce 2 separate lines
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110517/720310ac/attachment.html>


More information about the Tutor mailing list