[Tutor] Taking FASTA file as an input in Python 3

Alan Gauld alan.gauld at yahoo.co.uk
Mon Oct 21 17:54:49 EDT 2019


On 21 October 2019, at 21:54, Mihir Kharate <kharatemihir at gmail.com> wrote:

>with open (fasta , "r") as DNA_sequence:
>     DNA_sequence.readline()  # throw away first line
>     print ("DNA_sequence: \n")
>     for line in DNA_sequence:
>         x = sys.stdout.write(line)

More that the returnable from write is the number of chats written, is a number. It is not normally very useful.

>
>I used the sys,stdout.write() to remove any blank spaces and indentations

Write does not do that. It just writes out the string which is given with no changes.


>sys.stdout.write() converts the text into integer

No. It returns an integer as discussed above. The number of characters written.


>When I run the program, it simply prints out the sequence, regardless of
>whether or not I make a call to print the variable it is assigned to.

That's what sys.stdout.Write does. It writes the line to stdout.


>  Eventually my goal is to be
>able to take the fasta file as an input ---> overread the first line --->
>convert the rest of the text as a continuous string --> store this string
>into a variable,.. so that I can use it to do other things.

In that case you need to build the string from the input. The best way to do that is store the lines in a list then use string.join to create a single string from the list contents.

In pseudo code...

With open datafile as f
     data = []
     F.readline
     For line in c:
          data.append(line)
     S = "\n".join(data)

You'll need to convert that to value python but the structure should work.


Alan g


More information about the Tutor mailing list