Skip to content
Advertisement

Python script to extract data from csv file

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.

JavaScript

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!

Advertisement

Answer

Create a boolean variable

JavaScript

And a List of south and its descendants

JavaScript

And replace your If and Elif with this

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement