readline() & seek() ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DataSmash

    readline() & seek() ???

    Hi group,
    I have a text file that contains thousands of lines and each line is
    256 characters long.

    This is my task:
    For each line in the file, move to the 25th character, if the
    character is a "T",
    move to the 35th character of the line and read 5 characters from
    there.
    Capture these 5 characters and write them to a new text file, each 5
    characters separated by a comma.

    I appreciate your help!
    R.D.

  • Tim Roberts

    #2
    Re: readline() & seek() ???

    DataSmash <rdh@new.rr.com wrote:
    >
    >I have a text file that contains thousands of lines and each line is
    >256 characters long.
    >
    >This is my task:
    >For each line in the file, move to the 25th character, if the
    >character is a "T",
    >move to the 35th character of the line and read 5 characters from
    >there.
    >Capture these 5 characters and write them to a new text file, each 5
    >characters separated by a comma.
    >
    >I appreciate your help!
    Did you even TRY this? Your task reads like pseudocode that translates
    virtually line-for-line to Python code.

    fout = open('outputfil e.txt','w')
    for line in open('inputfile .txt'):
    if line[24] == 'T':
    fout.write( line[34:39] + ',' )
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • Carl Banks

      #3
      Re: readline() &amp; seek() ???

      On Jun 4, 5:30 pm, DataSmash <r...@new.rr.co mwrote:
      Hi group,
      I have a text file that contains thousands of lines and each line is
      256 characters long.
      >
      This is my task:
      For each line in the file, move to the 25th character, if the
      character is a "T",
      move to the 35th character of the line and read 5 characters from
      there.
      Capture these 5 characters and write them to a new text file, each 5
      characters separated by a comma.
      Your professor possibly reads comp.lang.pytho n, and if so, is likely
      to know how to track you down with your IP address.


      Carl Banks

      Comment

      • DataSmash

        #4
        Re: readline() &amp; seek() ???

        On Jun 5, 3:50 am, Carl Banks <pavlovevide... @gmail.comwrote :
        On Jun 4, 5:30 pm, DataSmash <r...@new.rr.co mwrote:
        >
        Hi group,
        I have a text file that contains thousands of lines and each line is
        256 characters long.
        >
        This is my task:
        For each line in the file, move to the 25th character, if the
        character is a "T",
        move to the 35th character of the line and read 5 characters from
        there.
        Capture these 5 characters and write them to a new text file, each 5
        characters separated by a comma.
        >
        Your professor possibly reads comp.lang.pytho n, and if so, is likely
        to know how to track you down with your IP address.
        >
        Carl Banks

        Marc, Thanks.

        Tim, Thanks for the code. It's a easy task IF you know what to look
        for. I didn't.

        Carl, I'm not a student. Was just looking for some ideas.

        Comment

        • Kam-Hung Soh

          #5
          Re: readline() &amp; seek() ???

          Tim Roberts wrote:
          DataSmash <rdh@new.rr.com wrote:
          >I have a text file that contains thousands of lines and each line is
          >256 characters long.
          >>
          >This is my task:
          >For each line in the file, move to the 25th character, if the
          >character is a "T",
          >move to the 35th character of the line and read 5 characters from
          >there.
          >Capture these 5 characters and write them to a new text file, each 5
          >characters separated by a comma.
          >>
          >I appreciate your help!
          >
          Did you even TRY this? Your task reads like pseudocode that translates
          virtually line-for-line to Python code.
          >
          fout = open('outputfil e.txt','w')
          for line in open('inputfile .txt'):
          if line[24] == 'T':
          fout.write( line[34:39] + ',' )
          Should the last line be ...

          fout.write(','. join(line[34:39])

          --
          Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

          Comment

          • Chris

            #6
            Re: readline() &amp; seek() ???

            On Jun 6, 5:13 am, Kam-Hung Soh <kamhung....@gm ail.comwrote:
            Tim Roberts wrote:
            DataSmash <r...@new.rr.co mwrote:
            I have a text file that contains thousands of lines and each line is
            256 characters long.
            >
            This is my task:
            For each line in the file, move to the 25th character, if the
            character is a "T",
            move to the 35th character of the line and read 5 characters from
            there.
            Capture these 5 characters and write them to a new text file, each 5
            characters separated by a comma.
            >
            I appreciate your help!
            >
            Did you even TRY this?  Your task reads like pseudocode that translates
            virtually line-for-line to Python code.
            >
              fout = open('outputfil e.txt','w')
              for line in open('inputfile .txt'):
                  if line[24] == 'T':
                      fout.write( line[34:39] + ',' )
            >
            Should the last line be ...
            >
            fout.write(','. join(line[34:39])
            >
            --
            Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
            each 5 characters need to be delimited by a comma, your statement
            would have a comma between each of the 5 characters.

            Comment

            • Kam-Hung Soh

              #7
              Re: readline() &amp; seek() ???

              Chris wrote:
              On Jun 6, 5:13 am, Kam-Hung Soh <kamhung....@gm ail.comwrote:
              >Tim Roberts wrote:
              >>DataSmash <r...@new.rr.co mwrote:
              >>>I have a text file that contains thousands of lines and each line is
              >>>256 characters long.
              >>>This is my task:
              >>>For each line in the file, move to the 25th character, if the
              >>>character is a "T",
              >>>move to the 35th character of the line and read 5 characters from
              >>>there.
              >>>Capture these 5 characters and write them to a new text file, each 5
              >>>characters separated by a comma.
              >>>I appreciate your help!
              >>Did you even TRY this? Your task reads like pseudocode that translates
              >>virtually line-for-line to Python code.
              >> fout = open('outputfil e.txt','w')
              >> for line in open('inputfile .txt'):
              >> if line[24] == 'T':
              >> fout.write( line[34:39] + ',' )
              >Should the last line be ...
              >>
              >fout.write(',' .join(line[34:39])
              >>
              >--
              >Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>
              >
              each 5 characters need to be delimited by a comma, your statement
              would have a comma between each of the 5 characters.
              You're right; I see where I got confused.

              --
              Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

              Comment

              Working...