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 3943486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:47:15+00:00 2026-05-20T00:47:15+00:00

I am trying to write a generic function to be able to read a

  • 0

I am trying to write a generic function to be able to read a fixed length file. I could go through and hand count the length between columns, and then read the file, but I was wondering if there was a way to do it programmatically.

I can see what needs to be done, but I am not sure the proper way to do it…

If I have a file like:

 ColA  ColB       ColC      FinalCol
    1    22         23 ColumnsCnBTxt
  213     1          2             2
11213 11111 1234567890             3

All of the headers are “right justified” and seperated by spaces (not tabs), so I basically just need to count from StartIndex to last Character and that is my column length.

Is there any easy way to achieve this in python? The resulting object would be a list of column lengths

header_line = " ColA  ColB       ColC      FinalCol"
result = get_header_information(header_line)
#result  = (5,5, 10, 13)
  • 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-20T00:47:16+00:00Added an answer on May 20, 2026 at 12:47 am

    One-liner using regex splits:

    >>> map(len, re.split(r"(?<=[^ ]) ", head))
    [5, 5, 10, 13]
    

    Explanation:

    re.split splits a string at all points where a regular expression matches. The regular expression I use (others would be possible) has a lookbehind group (?<=[^ ]) which means “preceded by a non-space” and then a space, so matches spaces which are preceded by non-spaces. This will split the string into the column headers, and then we simply take the lengths of the resulting strings.

    Notice that this is not performance-optimal — we are making three passes through the string and invoking a regex engine — but for normal-size strings that’s fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a generic function to start and end events into my
I am trying to write a generic Parse method that converts and returns a
I'm trying to write a regex function that will identify and replace a single
I'm trying to write some PHP to upload a file to a folder on
Hey I'm trying to write a function that calls a static function based upon
I'm trying to write some generic http response handler functions that sometimes open UIAlertViews.
What I'm trying to do is to write a generic rss reader that I
I'm trying write a query to find records which don't have a matching record
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
I'm trying to write a blog post which includes a code segment inside a

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.