As basis for the assignment, we use a CSV file named country.csv in which information about
countries is recorded. This file is posted on the assignment Web page1. 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 the name of the capital, the third is the total area (in km2), the fourth is the ISO abbreviation for the country’s name, and the last one gives the country’s population. Take a look at
the file using some standard text editor such as Notepad.
Your program has to determine the name and population of all those countries whose area is below
or equal to a number entered by the user. The names of all those countries including their population
needs to be written to a file named results.txt. That is, your program does not print the result
on screen but writes it to the file results.txt.
Here is a sample session:
Please enter the maximum total area: 7
6 countries found.
Please open the file results.txt to see the results.
Based on the above input, your program should create the file results.txt with the following
content.
Gibraltar 27833
Holy See (Vatican City) 921
Johnston Atoll 396
Juan de Nova Island 10
Kingman Reef 5
Monaco 32270

Recommended Answers

All 4 Replies

Member Avatar for kdoiron

Well, that sounds easy enough. What have you got so far? Remember what it said at the top of the Python forum page? We'll help you out, not do your homework for you.

CSV files are generated by spread sheet programs. Rows make a line in the file and the data columns are separated by commas. Python has a module called csv that makes processing this type of file much easier. Check the Python manual.

this is wht i have so far.. but still having some trouble with this..

a = input("Please enter the maximum total area: ")
infile = open("result.csv", "r")
outfile = open("results.txt", "w")

counter = 0

for line in infile:
if "NAME" in line:
continue
counter +=1
line = line.rstrip()
fields = line.split(',')
name, capital, area, iso, population = fields
if a >= area:
print name, capital, population

Member Avatar for kdoiron

First, please use code tags around your code - indentation is important in Python, and invisibl without the code tags.

Don't use input - use raw_input instead, and then use 'float' to convert your answer to a float number, so you can compare it to the area in your spreadsheet (which you should also convert to a number using float). The reasons to not use input are somewhat complex for a beginner, but it's worth getting into good habits from the beginning.

Next, I wouldn't use the test you do to see whether "NAME" is in the input line. What if that character string exists in a country name, such as Suriname? You already know that the word "NAME" will appear in the country name field of the spreadsheet, so why not look for it there, after you've split it out.

The instructions say to put the output to a file, so "print" isn't what you want.

Not a bad start, actually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.