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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:44:08+00:00 2026-06-12T14:44:08+00:00

I have a huge file, which has some missing rows. The data needs to

  • 0

I have a huge file, which has some missing rows. The data needs to be rooted at Country.

The input data is like:

csv_str = """Type,Country,State,County,City,
1,USA,,,
2,USA,OH,,
3,USA,OH,Franklin,
4,USA,OH,Franklin,Columbus
4,USA,OH,Franklin,Springfield
4,USA,WI,Dane,Madison
"""

which needed to be:

csv_str = """Type,Country,State,County,City,
1,USA,,,
2,USA,OH,,
3,USA,OH,Franklin,
4,USA,OH,Franklin,Columbus
4,USA,OH,Franklin,Springfield
4,USA,WI,,
4,USA,WI,Dane,
4,USA,WI,Dane,Madison
"""

The key as per my logic is Type field, where if I cannot find a County (type 3) for a City (type 4), then insert a row with fields upto County.

Same with County. If I cannot find a State (type 2) for a County (type 3), then insert a row with fields upto State.

With my lack of understanding the facilities in python, I was trying more of a brute-force approach. It is bit problematic as I need lot of iteration over the same file.

I was also tried google-refine, but couldn’t get it work. Doing manually is quite error prone.

Any help appreciated.

import csv
import io

csv_str = """Type,Country,State,County,City,
1,USA,,,
2,USA,OH,,
3,USA,OH,Franklin,
4,USA,OH,Franklin,Columbus
4,USA,OH,Franklin,Springfield
4,USA,WI,Dane,Madison
"""
found_county =[]
missing_county =[]

def check_missing_county(row):
    found = False
    for elm in found_county:
        if elm.Type == row.Type:
            found = True
    if not found:
        missing_county.append(row)
        print(row)

reader = csv.reader(io.StringIO(csv_str))
for row in reader:
    check_missing_county(row)
  • 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-12T14:44:11+00:00Added an answer on June 12, 2026 at 2:44 pm

    I’ve knocked up the following based on my understanding of the question:

    import csv
    import io
    
    csv_str = u"""Type,Country,State,County,City,
    1,USA,,,
    2,USA,OH,,
    3,USA,OH,Franklin,
    4,USA,OH,Franklin,Columbus
    4,USA,OH,Franklin,Springfield
    4,USA,WI,Dane,Madison
    """
    
    counties = []
    states = []
    
    
    def handle_missing_data(row):
        try:
            rtype = int(row[0])
        except ValueError:
            return []
    
        rtype = row[0]
        country = row[1]
        state = row[2]
        county = row[3]
    
        rows = []
        # if a state is present and it hasn't a row of it's own
        if state and state not in states:
            rows.append([rtype, country, state, '', ''])
            states.append(state)
    
        # if a county is present and it hasn't a row of it's own
        if county and county not in counties:
            rows.append([rtype, country, state, county, ''])
            counties.append(county)
    
        # if the row hasn't already been added add it now
        if row not in rows:
            rows.append(row)
    
        return rows
    
    csvf = io.StringIO(csv_str)
    reader = csv.reader(csvf)
    for row in reader:
        new_rows = handle_missing_data(row)
        for new_row in new_rows:
            print new_row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an excel sheet which has some huge data. Data is organized as
I have a huge file that has some lines that need to have a
I have a huge pdf file (20 mb/800 pages) which contains some information. It
I have a html file which has a huge table with 4 columns and
I have a file which has text like this: #1#14#ADEADE#CAH0F#0#0..... I need to create
I have a huge text file which I want to explode into an array.
I currently have a huge HTML file which doesn't have line breaks and just
I have a huge access mdb file which contains a single table with 20-30
I have a very huge file in which I need to obtain every nth
I have a huge C file (~100k lines) which I need to be able

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.