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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:38:44+00:00 2026-05-26T00:38:44+00:00

I have tables which looks like this: text = ID = 1234 Hello World

  • 0

I have tables which looks like this:

text = """
ID = 1234

Hello World              135,343    117,668    81,228
Another line of text    (30,632)              (48,063)
More text                  0         11,205       0    
Even more text                       1,447       681

ID = 18372

Another table                        35,323              38,302      909,381
Another line with text                 13                  15
More text here                                              7           0    
Even more text here                   7,011               1,447        681
"""

Is there a way to replace the “blank” entries in each table with 0? I am trying to set delimiters between the entries, but using the following code can’t deal with blank spots in the tables:

for line in text.splitlines():
    if 'ID' not in line:
        line1 = line.split()
        line = '|'.join((' '.join(line1[:-3]), '|'.join(line1[-3:])))
        print line
    else:
        print line

The output is:

ID = 1234
|
Hello World|135,343|117,668|81,228
Another line of|text|(30,632)|(48,063)
More text|0|11,205|0
Even more|text|1,447|681
|
ID = 18372
|
Another table|35,323|38,302|909,381
Another line with|text|13|15
More text|here|7|0
Even more text here|7,011|1,447|681

As you can see, the first problem shows up on the second line of the first table. The word ‘text’ is considered the first column. Any way to fix this in Python to replace blank entries with 0?

  • 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-26T00:38:44+00:00Added an answer on May 26, 2026 at 12:38 am

    Here is a function for finding columns in a bunch of lines. The second argument pat defines what a column is, and can be any regex.

    import itertools as it
    import re
    
    def find_columns(lines, pat = r' '):
        '''
        Usage:
        widths = find_columns(lines)
        for line in lines:
            if not line: continue
            vals = [ line[widths[i]:widths[i+1]].strip() for i in range(len(widths)-1) ]
        '''
        widths = []
        maxlen = max(len(line) for line in lines)
        for line in lines:
            line = ''.join([line, ' '*(maxlen-len(line))])
            candidates = []
            for match in re.finditer(pat, line):
                candidates.extend(range(match.start(), match.end()+1))
            widths.append(set(candidates))
        widths = sorted(set.intersection(*widths))
        diffs = [widths[i+1]-widths[i] for i in range(len(widths)-1)]
        diffs = [None]+diffs
        widths = [w for d, w in zip(diffs, widths) if d != 1]
        if widths[0] != 0: widths = [0]+widths
        return widths
    
    def report(text):
        for key, group in it.groupby(text.splitlines(), lambda line:line.startswith('ID')):
            lines = list(group)
            if key:
                print('\n'.join(lines))
            else:
                # r' (?![a-zA-Z])' defines a column to be any whitespace
                # not followed by alphabetic characters.
                widths = find_columns(lines, pat = r'\s(?![a-zA-Z])')
                for line in lines:
                    if not line: continue
                    vals = [ line[widths[i]:widths[i+1]] for i in range(len(widths)-1) ]
                    vals = [v if v.strip() else v[1:]+'0' for v in vals]
                    print('|'.join(vals))
    
    text = """\
    ID = 1234
    
    Hello World              135,343    117,668    81,228
    Another line of text    (30,632)              (48,063)
    More text                  0         11,205       0    
    Even more text                       1,447       681
    
    ID = 18372
    
    Another table                        35,323              38,302      909,381
    Another line with text                 13                  15
    More text here                                              7           0    
    Even more text here                   7,011               1,447        681
    """
    
    report(text)
    

    yields

    ID = 1234
    Hello World         |     135,343|    117,668|    81,228
    Another line of text|    (30,632)|          0|   (48,063)
    More text           |       0    |     11,205|       0   
    Even more text      |           0|     1,447 |      681
    ID = 18372
    Another table         |               35,323|              38,302|      909,381
    Another line with text|                 13  |                15|0
    More text here        |                    0|                 7  |         0   
    Even more text here   |                7,011|               1,447|        681
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables which looks something like this Table Queue int ID; string
I have a csv file which looks like this $lines[0] = text, with commas,
I have a table which looks like this: <table id='t'> <thead> .... </thead> <tbody
I have a couple of tables which look like this Table 1 user_id |
I have 2 tables which in simplified form look like this: Products( id: int,
I have this mysql table called comments which looks like this: commentID parentID type
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
I have a table in SQL Server which looks like this: ID Code Name
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have gridview in my asp.net 3.5 application [C#]. Which looks like this: <asp:GridView

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.