[Tutor] String Processing Query

Alan Gauld alan.gauld at btinternet.com
Tue May 17 12:55:58 CEST 2011


"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'))

string1 = ''.join(scan[1:-1]).replace('\n','')

Does that do it?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list