I have just started using python and am stuck on a particular project. I have been stuck staring at my computer not knowing what to do. I will post details below.
What I am trying to do is use a CSV file named country.csv in which information about countries is recorded. The first line in the file is the header line and describes the content of each column. The first value in a line is the name of the country, the second is an abbreviation for the country’s name, the third is the name of the capital, the fourth is the total population, and the last one gives the total area of the country. The program has to determine the names and capital of all those countries whose population is above a number entered by the user. The names of all those countries including their capital needs to be written to a file named results.txt.
Here is what it should look like:
Please enter the minimum population: 200000000
4 Records found.
Please open the file results.txt to see the results.
Based on the above input, the program should create the file results.txt with the following
content.
China Beijing
India New Delhi
Indonesia Jakarta
United States Washington DC
The file I am reading from looks like this:
NAME,ISO,CAPITAL,POPULATION,AREA
Afghanistan ,AF ,Kabul,28513677,647500
Albania ,AL ,Episkopi,3544808,28748
Algeria ,AG ,Tirana,32129324,2381740
American Samoa ,AQ ,Algiers,57902,199
Andorra ,AN ,Pago Pago,69865,468
Angola ,AO ,Andorra la Vella,10978552,1246700
I believe I should be using a for loop and if statement. Here is what I have so far (probably way wrong).
p = input("Please enter the minimum population: ")
infile = open("country.csv", "r")
outfile = open("results.txt", "w")
for line in infile:
fields = line.split(",")
if fields >= p:
print fields
Any help would really be great!