•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 426,659 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,523 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 302 | Replies: 4
![]() |
•
•
Join Date: Apr 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: May 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
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.
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.
But I don't like SPAM!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Python Forum
- Previous Thread: Tkinter
- Next Thread: need help!



Linear Mode