User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2008
Posts: 4
Reputation: urmybaby is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
urmybaby urmybaby is offline Offline
Newbie Poster

Can anyone give some hint and idea about the file processing? thx

  #1  
May 14th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: Can anyone give some hint and idea about the file processing? thx

  #2  
May 14th, 2008
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.
But I don't like SPAM!
Reply With Quote  
Join Date: Aug 2005
Posts: 1,135
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 66
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Can anyone give some hint and idea about the file processing? thx

  #3  
May 14th, 2008
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.
drink her pretty
Reply With Quote  
Join Date: May 2008
Posts: 1
Reputation: hello420 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hello420 hello420 is offline Offline
Newbie Poster

Re: Can anyone give some hint and idea about the file processing? thx

  #4  
May 14th, 2008
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
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: Can anyone give some hint and idea about the file processing? thx

  #5  
May 14th, 2008
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.
But I don't like SPAM!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 1:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC