Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1002519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:50:34+00:00 2026-05-16T07:50:34+00:00

I have a text file which does not confirm to standards. So I know

  • 0

I have a text file which does not confirm to standards. So I know the (end,start) positions of each column value.

Sample text file :

#     #   #   #
Techy Inn Val NJ

Found the position of # using this code :

  1 f = open('sample.txt', 'r')
  2 i = 0
  3 positions = []
  4 for line in f:
  5     if line.find('#') > 0:
  6         print line
  7         for each in line:
  8             i += 1
  9             if each == '#':
 10                 positions.append(i)

1 7 11 15 => Positions

So far, so good! Now, how do I fetch the values from each row based on the positions I fetched? I am trying to construct an efficient loop but any pointers are greatly appreciated guys! Thanks (:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:50:34+00:00Added an answer on May 16, 2026 at 7:50 am

    Off the top of my head:

    f = open(.......)
    header = f.next() # get first line
    posns = [i for i, c in enumerate(header + "#") if c = '#']
    for line in f:
        fields = [line[posns[k]:posns[k+1]] for k in xrange(len(posns) - 1)]
    

    Update with tested, fixed code:

    import sys
    f = open(sys.argv[1])
    header = f.next() # get first line
    print repr(header)
    posns = [i for i, c in enumerate(header) if c == '#'] + [-1]
    print posns
    for line in f:
        posns[-1] = len(line)
        fields = [line[posns[k]:posns[k+1]].rstrip() for k in xrange(len(posns) - 1)]
        print fields
    

    Input file:

    #      #  #
    Foo    BarBaz
    123456789abcd
    

    Debug output:

    '#      #  #\n'
    [0, 7, 10, -1]
    ['Foo', 'Bar', 'Baz']
    ['1234567', '89a', 'bcd']
    

    Robustification notes:

    1. This solution caters for any old rubbish (or nothing) after the last # in the header line; it doesn’t need the header line to be padded out with spaces or anything else.
    2. The OP needs to consider whether it’s an error if the first character of the header is not #.
    3. Each field has trailing whitespace stripped; this automatically removes a trailing newline from the rihtmost field (and doesn’t run amok if the last line is not terminated by a newline).

    Final(?) update: Leapfrooging @gnibbler’s suggestion to use slice(): set up the slices once before looping.

    import sys
    f = open(sys.argv[1])
    header = f.next() # get first line
    print repr(header)
    posns = [i for i, c in enumerate(header) if c == '#']
    print posns
    slices = [slice(lo, hi) for lo, hi in zip(posns, posns[1:] + [None])]
    print slices
    for line in f:
        fields = [line[sl].rstrip() for sl in slices]
        print fields
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.