[Tutor] Help
Steven D'Aprano
steve at pearwood.info
Sat May 15 06:13:58 CEST 2010
On Fri, 14 May 2010 06:08:30 pm she haohao wrote:
> Say I have a .fa file and I want to print a subsequence from the file
> without the \n how can i do it.
>
> Example: Inside the test.fa file I have
>
> >chromosome 1
>
> ACTGTGTTC
> ACGTCGACC
> AVGTTTTTT
> ACGTTaGTC
>
> so if I say i wan the subsequence starting from the 7th character to
> the 11th(i.e when i let p=(7+11)/2=9 and v=2) character.(excluding
> the first line), mean I want TTCAC.
>
> So how should I write the code so that \n does not appear if i want
> p=10 and v=3.
text = open('test.fa', 'r').read() # includes newlines
text = text.replace('\n', '') # remove newlines
To get the characters 7 to 11 inclusive "TTCAC", you have to remember
that Python starts counting at 0, not 1, and the second position is
excluded. So you have to write:
text[6:11]
to get TTCAC.
--
Steven D'Aprano
More information about the Tutor
mailing list