File Processing in Python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 5
Reputation: zclevenger is an unknown quantity at this point 
Solved Threads: 0
zclevenger zclevenger is offline Offline
Newbie Poster

File Processing in Python

 
0
  #1
Feb 16th, 2008
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!
Last edited by zclevenger; Feb 16th, 2008 at 9:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: File Processing in Python

 
0
  #2
Feb 16th, 2008
Here is a hint ...
  1. """
  2. Part of typical csv file (Country.csv):
  3. NAME,ISO,CAPITAL,POPULATION,AREA
  4. Afghanistan ,AF ,Kabul,28513677,647500
  5. Albania ,AL ,Episkopi,3544808,28748
  6. Algeria ,AG ,Tirana,32129324,2381740
  7. American Samoa ,AQ ,Algiers,57902,199
  8. Andorra ,AN ,Pago Pago,69865,468
  9. Angola ,AO ,Andorra la Vella,10978552,1246700
  10. """
  11.  
  12. for line in file("Country.csv"):
  13. # skip header
  14. if 'NAME' in line:
  15. continue
  16. # remove trailing newline char
  17. line = line.rstrip()
  18. # convert each line to a list
  19. clist = line.split(',')
  20. # unpack the list
  21. name, iso, capital, population, area = clist
  22. # population conditional
  23. if int(population) >= 20000000:
  24. print name, capital
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: zclevenger is an unknown quantity at this point 
Solved Threads: 0
zclevenger zclevenger is offline Offline
Newbie Poster

Re: File Processing in Python

 
0
  #3
Feb 17th, 2008
This is what i got, but stuck again. And I want to try and write results to another file. Any pointers would help.

p = input("Please enter the minimum population: ")
infile = open("country.csv", "r")
outfile = open("results.txt", "w")

for line in infile:
if 'name' in line:
continue
line = line.rstrip()
clist = line.split(',')
clist = name, iso, capital, population, area
if int (population) >= p:
outfile.write(name, capital)
outfile.close()


outfile.close()
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: File Processing in Python

 
0
  #4
Feb 17th, 2008
Please use code tags around your code so it will retain the indentations!

You are also unpacking clist wrong, also
write() writes a string to file, so concatinate first.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: zclevenger is an unknown quantity at this point 
Solved Threads: 0
zclevenger zclevenger is offline Offline
Newbie Poster

Re: File Processing in Python

 
0
  #5
Feb 18th, 2008
Ok. I'm on the right track. 2 questions:
1. What code do I use to tell user how many records were found?
2. How do I write results to file rather than print them?

Here is what I got:

  1. p = input("Please enter the minimum population: ")
  2. infile = open("country.csv", "r")
  3. outfile = open("results.txt", "w")
  4.  
  5. #
  6. for line in infile:
  7. if "NAME" in line:
  8. continue
  9. line = line.rstrip()
  10. fields = line.split(',')
  11. name, iso, capital, population, area = fields
  12. if int(population) >= p:
  13. print name, capital
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: File Processing in Python

 
0
  #6
Feb 18th, 2008
1. Usually, you use a counter like this:

  1. counter = 0 # initialize counter here
  2.  
  3. for line in infile:
  4. if "NAME" in line:
  5. continue
  6. counter += 1 # increment counter here
  7. line = line.rstrip()
  8. fields = line.split(',')
  9. name, iso, capital, population, area = fields
  10. if int(population) >= p:
  11. print name, capital
  12.  
  13. print "Total countries: %d" % counter

2. Here's a possibility:

  1. counter = 0 # initialize counter here
  2. f = open("myoutputfile","w") # look up the open() function in the docs
  3.  
  4. for line in infile:
  5. if "NAME" in line:
  6. continue
  7. counter += 1 # increment counter here
  8. line = line.rstrip()
  9. fields = line.split(',')
  10. name, iso, capital, population, area = fields
  11. if int(population) >= p:
  12. f.write(name + " " + capital + "\n")
  13.  
  14. f.write("Total countries: %d\n" % counter)
  15. f.close()

Note the extra \n characters needed.

Jeff
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3843 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC