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!

Here is a hint ...

"""
Part of typical csv file (Country.csv):
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
"""

for line in file("Country.csv"):
    # skip header
    if 'NAME' in line:
        continue
    # remove trailing newline char
    line = line.rstrip()
    # convert each line to a list
    clist = line.split(',')
    # unpack the list
    name, iso, capital, population, area = clist
    # population conditional
    if int(population) >= 20000000:
        print name, capital

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()

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.

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:

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()
    fields = line.split(',')
    name, iso, capital, population, area = fields
    if int(population) >= p:
        print name, capital

1. Usually, you use a counter like this:

counter = 0  # initialize counter here

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

print "Total countries: %d" % counter

2. Here's a possibility:

counter = 0  # initialize counter here
f = open("myoutputfile","w")   # look up the open() function in the docs

for line in infile:
    if "NAME" in line:
        continue
    counter += 1  # increment counter here
    line = line.rstrip()
    fields = line.split(',')
    name, iso, capital, population, area = fields
    if int(population) >= p:
        f.write(name + " " + capital + "\n")

f.write("Total countries: %d\n" % counter)
f.close()

Note the extra \n characters needed.

Jeff

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.