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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:01:04+00:00 2026-06-17T23:01:04+00:00

Hi I have an excel file with a similar structure as below: Location column2

  • 0

Hi I have an excel file with a similar structure as below:

      Location       column2    column3
1     South Africa
2     
3     
4     
5     England
6     
7     
8     
9     U.S
10    
11    
12    

I am trying to write a python script that can fill out the spaces between each location with the name of the preceding location (i.e fill out the space from 2 to 4 with the South Africa as the location, 6-8 will be filled out with England as the location, etc)

I would be grateful if someone could point me in the right direction.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-06-17T23:01:05+00:00Added an answer on June 17, 2026 at 11:01 pm

    Ok dude, I think the answer is this dumb wrapper I made for xlrd (or, one you write yourself!). The key is that the function reads one row at a time into a list, and that Python lists remember the order in which they were populated. The wrapper produces a dictionary which maps Excel sheet names to a list of rows on that sheet (we’re assuming one table per sheet here, you’ll have to generalize things otherwise). Each row is a dictionary whose keys are the column names.

    For you, I’d read in your data, and then do something like this (not tested):

    import see_below as sb
    dict = sb.workbookToDict(your_file)
    output = []
    this_location = None
    for row in dict[relevant_sheet_name]:
        output_row = row
        if row['Location'] is not None:
            this_location = row['Location']
        else:
            output_row['Location'] = this_location
    

    There might be something cute you can do with list comprehension, but I’ve had too much wine to fool with that tonight 🙂

    Here’s the wrapper for the reader:

    import xlrd
    
    
    def _isEmpty(_):
            return ''
    
    
    def _isString(element):
            return element.value.encode('ascii', 'ignore')
    
    
    def _isFloat(element):
        return float(element.value)
    
    
    def _isDate(element):
        import datetime
        rawDate = float(element.value)
        return (datetime.datetime(1899, 12, 30) +
                datetime.timedelta(days=rawDate))
    
    
    def _isBool(element):
        return element.value == 1
    
    
    def _isExcelGarbage(element):
        return int(element.value)
    
    
    _options = {0: _isEmpty,
                1: _isString,
                2: _isFloat,
                3: _isDate,
                4: _isBool,
                5: _isExcelGarbage,
                6: _isEmpty}
    
    
    def WorkbookToDict(filename):
        '''
            Reads .xlsx file into dictionary.
    
            The keys of the dictionary correspond to sheet names in the Excel workbook.
            The first row of the Excel workbook is taken to be column names, and each row
            of the worksheet is read into a separate dictionary, whose keys correspond to
            column names. The collection of dictionaries (as a list) forms the value in the
            dictionary. The output maps sheet names (keys) to a collection of dictionaries
            (value).
        '''
        book = xlrd.open_workbook(filename)
        allSheets = {}
        for s in book.sheets():
            thisSheet = []
            headings = [_options[x.ctype](x) for x in s.row(0)]
    
            for i in range(s.nrows):
                if i == 0:
                    continue
    
                thisRow = s.row(i)
                if len(thisRow) != len(headings):
                    raise Exception("Mismatch between headings and row length in ExcelReader")
    
                rowDict = {}
                for h, r in zip(headings, thisRow):
                    rowDict[h] = _options[r.ctype](r)
                thisSheet.append(rowDict)
            allSheets[str(s.name)] = thisSheet
        return allSheets
    

    The writer is here:

    import xlwt
    
    def write(workbookDict, colMap, filename):
        '''
           workbookDict should be a map of sheet names to a list of dictionaries.
           Each member of the list should be a mapping of column names to contents,
           missing keys are handled with the nullEntry field. colMap should be a
           dictionary whose keys are identical tto the sheet names in the workbookDict.
           Each value is a list of column names that are assumed to be in order.
           If a key exists in the workbookDict that does not exist in the colDict, the
           entry in workbookDict will not be written.
        '''
    
        workbook = xlwt.Workbook()
    
        for sheet in workbookDict.keys():
            worksheet = workbook.add_sheet(sheet)
            cols = colMap[sheet]
            i = 0
            writeCols = True
            while i <= len(workbookDict[sheet]):
                if writeCols:
                    for j in range(len(cols)):
                        if writeCols:  # write col headings
                            worksheet.write(i, j, cols[j])
                    writeCols = False
                else:
                    for j in range(len(cols)):
                        worksheet.write(i, j, workbookDict[sheet][(i-1)][cols[j]])
                i += 1
    
        workbook.save(filename)
    

    Anyway, I really hope this works for you!

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

Sidebar

Related Questions

I Have an Excel 2003 file with a line similar to this: I need
I have an excel file, validating one column to enter numbers only but it
I have a excel file which is as follows . 1 Kr Veerappan 123
I have an excel file and I want to read each column and create
I have an excel file that I need to make some changes. I need
I have an Excel file that I need to import into a (new) Oracle
I have an Excel file with one column which is filled with numbers. I
I have an excel file that I want to embed in my C# assembly.
I have an Excel file that gets external data from database table. I need
I have an excel file which is refreshing data every few seconds and running

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.