Opening python file with readlines() method












-6















The folder with this project contains a plain text file with a DNA sequence in fasta format ('MYH7.fasta').



Complete the cell below to open and read the file using the readlines() method and to save the sequence to a string named DNAsequence.



NB: The parts to be completed are indicated with "...". The other parts should not be changed.



inFile = ...

DNAsequence = ''

for ... in ...:

if not(">" in Line):

if (len(Line) > 0):

DNAsequence = DNAsequence + Line

print(DNAsequence)

print(len(DNAsequence))









share|improve this question

























  • Try to improve your questions with a code that can be executed by others. Minimal, Complete, and Verifiable example

    – Daniel GL
    Nov 22 '18 at 12:21






  • 2





    @DanielGL: but this is not a question. It is an assignment.

    – usr2564301
    Nov 22 '18 at 12:22
















-6















The folder with this project contains a plain text file with a DNA sequence in fasta format ('MYH7.fasta').



Complete the cell below to open and read the file using the readlines() method and to save the sequence to a string named DNAsequence.



NB: The parts to be completed are indicated with "...". The other parts should not be changed.



inFile = ...

DNAsequence = ''

for ... in ...:

if not(">" in Line):

if (len(Line) > 0):

DNAsequence = DNAsequence + Line

print(DNAsequence)

print(len(DNAsequence))









share|improve this question

























  • Try to improve your questions with a code that can be executed by others. Minimal, Complete, and Verifiable example

    – Daniel GL
    Nov 22 '18 at 12:21






  • 2





    @DanielGL: but this is not a question. It is an assignment.

    – usr2564301
    Nov 22 '18 at 12:22














-6












-6








-6


0






The folder with this project contains a plain text file with a DNA sequence in fasta format ('MYH7.fasta').



Complete the cell below to open and read the file using the readlines() method and to save the sequence to a string named DNAsequence.



NB: The parts to be completed are indicated with "...". The other parts should not be changed.



inFile = ...

DNAsequence = ''

for ... in ...:

if not(">" in Line):

if (len(Line) > 0):

DNAsequence = DNAsequence + Line

print(DNAsequence)

print(len(DNAsequence))









share|improve this question
















The folder with this project contains a plain text file with a DNA sequence in fasta format ('MYH7.fasta').



Complete the cell below to open and read the file using the readlines() method and to save the sequence to a string named DNAsequence.



NB: The parts to be completed are indicated with "...". The other parts should not be changed.



inFile = ...

DNAsequence = ''

for ... in ...:

if not(">" in Line):

if (len(Line) > 0):

DNAsequence = DNAsequence + Line

print(DNAsequence)

print(len(DNAsequence))






python python-3.x python-requests jupyter-notebook






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:26









Rarblack

2,85241025




2,85241025










asked Nov 22 '18 at 12:13









C.Zennaro C.Zennaro

11




11













  • Try to improve your questions with a code that can be executed by others. Minimal, Complete, and Verifiable example

    – Daniel GL
    Nov 22 '18 at 12:21






  • 2





    @DanielGL: but this is not a question. It is an assignment.

    – usr2564301
    Nov 22 '18 at 12:22



















  • Try to improve your questions with a code that can be executed by others. Minimal, Complete, and Verifiable example

    – Daniel GL
    Nov 22 '18 at 12:21






  • 2





    @DanielGL: but this is not a question. It is an assignment.

    – usr2564301
    Nov 22 '18 at 12:22

















Try to improve your questions with a code that can be executed by others. Minimal, Complete, and Verifiable example

– Daniel GL
Nov 22 '18 at 12:21





Try to improve your questions with a code that can be executed by others. Minimal, Complete, and Verifiable example

– Daniel GL
Nov 22 '18 at 12:21




2




2





@DanielGL: but this is not a question. It is an assignment.

– usr2564301
Nov 22 '18 at 12:22





@DanielGL: but this is not a question. It is an assignment.

– usr2564301
Nov 22 '18 at 12:22












1 Answer
1






active

oldest

votes


















0














I suspect the answer they are looking for is



inFile = open("MYH7.fasta", "r").readlines()


In Perl concatenating code is standard (example below). I'm not sure here because inFile.readlines() should construct an array based on carriage returns and the invariable code in your question doen't allow its functionality to be used. The following code answers your question leaving the file handle open and omits readlines().



#!/usr/local/bin/python3
inFile = open("MYH7.fasta", "r")

DNAsequence = ''

for Line in inFile:
if not(">" in Line):
if (len(Line) > 0):
DNAsequence = DNAsequence + Line

print(DNAsequence)

print(len(DNAsequence))


The Perl version shows how several commands can be concatenated in a single line.



#!/usr/bin/perl
my $DNAsequence;
open (IN, "<MYH7.fasta");
while (my $line = <IN>){
chomp $line;
next if $line =~ m/>/;
$line =~ m/[ACGT]{10}/i ? $DNAsequence .= $line : next;
}
close IN;
print $DNAsequence, "n", length($DNAsequence) . " and Python s**ksn";


The MYH7.fasta input file will be of format,



>example_sequence
ACGCGTAGACGTATAG


I think there is a bug in your code because if carriage returns are present within the sequence they will carry through into the variable DNAsequence. In Perl these are removed using "chomp" or can by explicitly removed with $_ =~ s/n// (or in this eg $line =~ s/n//;). The presence of "n" could complicate subsequent analysis.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430781%2fopening-python-file-with-readlines-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I suspect the answer they are looking for is



    inFile = open("MYH7.fasta", "r").readlines()


    In Perl concatenating code is standard (example below). I'm not sure here because inFile.readlines() should construct an array based on carriage returns and the invariable code in your question doen't allow its functionality to be used. The following code answers your question leaving the file handle open and omits readlines().



    #!/usr/local/bin/python3
    inFile = open("MYH7.fasta", "r")

    DNAsequence = ''

    for Line in inFile:
    if not(">" in Line):
    if (len(Line) > 0):
    DNAsequence = DNAsequence + Line

    print(DNAsequence)

    print(len(DNAsequence))


    The Perl version shows how several commands can be concatenated in a single line.



    #!/usr/bin/perl
    my $DNAsequence;
    open (IN, "<MYH7.fasta");
    while (my $line = <IN>){
    chomp $line;
    next if $line =~ m/>/;
    $line =~ m/[ACGT]{10}/i ? $DNAsequence .= $line : next;
    }
    close IN;
    print $DNAsequence, "n", length($DNAsequence) . " and Python s**ksn";


    The MYH7.fasta input file will be of format,



    >example_sequence
    ACGCGTAGACGTATAG


    I think there is a bug in your code because if carriage returns are present within the sequence they will carry through into the variable DNAsequence. In Perl these are removed using "chomp" or can by explicitly removed with $_ =~ s/n// (or in this eg $line =~ s/n//;). The presence of "n" could complicate subsequent analysis.






    share|improve this answer






























      0














      I suspect the answer they are looking for is



      inFile = open("MYH7.fasta", "r").readlines()


      In Perl concatenating code is standard (example below). I'm not sure here because inFile.readlines() should construct an array based on carriage returns and the invariable code in your question doen't allow its functionality to be used. The following code answers your question leaving the file handle open and omits readlines().



      #!/usr/local/bin/python3
      inFile = open("MYH7.fasta", "r")

      DNAsequence = ''

      for Line in inFile:
      if not(">" in Line):
      if (len(Line) > 0):
      DNAsequence = DNAsequence + Line

      print(DNAsequence)

      print(len(DNAsequence))


      The Perl version shows how several commands can be concatenated in a single line.



      #!/usr/bin/perl
      my $DNAsequence;
      open (IN, "<MYH7.fasta");
      while (my $line = <IN>){
      chomp $line;
      next if $line =~ m/>/;
      $line =~ m/[ACGT]{10}/i ? $DNAsequence .= $line : next;
      }
      close IN;
      print $DNAsequence, "n", length($DNAsequence) . " and Python s**ksn";


      The MYH7.fasta input file will be of format,



      >example_sequence
      ACGCGTAGACGTATAG


      I think there is a bug in your code because if carriage returns are present within the sequence they will carry through into the variable DNAsequence. In Perl these are removed using "chomp" or can by explicitly removed with $_ =~ s/n// (or in this eg $line =~ s/n//;). The presence of "n" could complicate subsequent analysis.






      share|improve this answer




























        0












        0








        0







        I suspect the answer they are looking for is



        inFile = open("MYH7.fasta", "r").readlines()


        In Perl concatenating code is standard (example below). I'm not sure here because inFile.readlines() should construct an array based on carriage returns and the invariable code in your question doen't allow its functionality to be used. The following code answers your question leaving the file handle open and omits readlines().



        #!/usr/local/bin/python3
        inFile = open("MYH7.fasta", "r")

        DNAsequence = ''

        for Line in inFile:
        if not(">" in Line):
        if (len(Line) > 0):
        DNAsequence = DNAsequence + Line

        print(DNAsequence)

        print(len(DNAsequence))


        The Perl version shows how several commands can be concatenated in a single line.



        #!/usr/bin/perl
        my $DNAsequence;
        open (IN, "<MYH7.fasta");
        while (my $line = <IN>){
        chomp $line;
        next if $line =~ m/>/;
        $line =~ m/[ACGT]{10}/i ? $DNAsequence .= $line : next;
        }
        close IN;
        print $DNAsequence, "n", length($DNAsequence) . " and Python s**ksn";


        The MYH7.fasta input file will be of format,



        >example_sequence
        ACGCGTAGACGTATAG


        I think there is a bug in your code because if carriage returns are present within the sequence they will carry through into the variable DNAsequence. In Perl these are removed using "chomp" or can by explicitly removed with $_ =~ s/n// (or in this eg $line =~ s/n//;). The presence of "n" could complicate subsequent analysis.






        share|improve this answer















        I suspect the answer they are looking for is



        inFile = open("MYH7.fasta", "r").readlines()


        In Perl concatenating code is standard (example below). I'm not sure here because inFile.readlines() should construct an array based on carriage returns and the invariable code in your question doen't allow its functionality to be used. The following code answers your question leaving the file handle open and omits readlines().



        #!/usr/local/bin/python3
        inFile = open("MYH7.fasta", "r")

        DNAsequence = ''

        for Line in inFile:
        if not(">" in Line):
        if (len(Line) > 0):
        DNAsequence = DNAsequence + Line

        print(DNAsequence)

        print(len(DNAsequence))


        The Perl version shows how several commands can be concatenated in a single line.



        #!/usr/bin/perl
        my $DNAsequence;
        open (IN, "<MYH7.fasta");
        while (my $line = <IN>){
        chomp $line;
        next if $line =~ m/>/;
        $line =~ m/[ACGT]{10}/i ? $DNAsequence .= $line : next;
        }
        close IN;
        print $DNAsequence, "n", length($DNAsequence) . " and Python s**ksn";


        The MYH7.fasta input file will be of format,



        >example_sequence
        ACGCGTAGACGTATAG


        I think there is a bug in your code because if carriage returns are present within the sequence they will carry through into the variable DNAsequence. In Perl these are removed using "chomp" or can by explicitly removed with $_ =~ s/n// (or in this eg $line =~ s/n//;). The presence of "n" could complicate subsequent analysis.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 1:08

























        answered Nov 22 '18 at 22:50









        Michael G.Michael G.

        2231316




        2231316
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430781%2fopening-python-file-with-readlines-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?