0

sharing the sample file screenshots, script I developed and other details below.

In the countries_source.csv file, I have a list of countries and I need a subset of its data created in mycountries.csv file until I hit the value "Asia" in the first column.

enter image description here

Using the below script, I was able to get the data till the 14th row - which is good. However, I need help with the below.

In the countries_source.csv file, I should get 17 to 21 rows as well based on the values in columns A and B - that is, every row that is a descendant of "South" (row 17) should be included in the mycountries.csv file as well. Other rows should be ignored.

import csv
import os

os.remove("C:/Users/Documents/Python Scripts/mycountries.csv")

with open("C:/Users/Documents/Python Scripts/countries_source.csv", "r") as source:
    csv_reader = csv.reader(source)
    lst=[]
    with open("C:/Users/Documents/Python Scripts/mycountries.csv", "w",newline='') as result:
        writer = csv.writer(result)
    #print(lst)
        for r in csv_reader:
            lst.append(r) 
        for ele in lst:
            if ele[0] != "Asia" :
                writer.writerow(ele)
            elif ele[0] == "Asia":
                break

Based on the data in countries_source.csv file and my requirement as discussed above, the expected result of mycountries.csv file screenshot is provided below.

enter image description here

Could you please help me with some ideas? - I'm new to coding, please excuse any obvious mistakes in the code. Thank you!

1 Answer 1

1

Create a boolean variable

check = True

And a List of south and its descendants

descendant = ["South"]

And replace your If and Elif with this

check = True
descendant = ["South"]

for ele in lst:
    if check:
        writer.writerow(ele)

    if ele[0] == "Asia":  
        check = False
    
    if ele[1] in descendants:
        writer.writerow(ele)
        descendant.append(ele[0])
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Athar - thank you. Is there a way to specify only "South" instead of manually updating the descendant list each time there is an update to the hierarchy.
@Vikas yeah it can be done ,i updated my answer
Hi Athar - excellent. Though this is a sample dataset and I have about 60 fields in my files - I was able to get it working using your logic. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.