| | |
File Processing in Python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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!
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.
Here is a hint ...
python Syntax (Toggle Plain Text)
""" 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
May 'the Google' be with you!
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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()
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()
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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. 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:
Python Syntax (Toggle Plain Text)
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
1. Usually, you use a counter like this:
2. Here's a possibility:
Note the extra \n characters needed.
Jeff
python Syntax (Toggle Plain Text)
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:
python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Problems teaching myself Python (Python)
- Big Text File Processing (Python)
- Quick question about text processing (Shell Scripting)
- Sentence Processing (Python)
- Directory Scanning (sort of) (Python)
- how to read spread sheet in Python? (Python)
- Running a 3rd party program from Python (Python)
Other Threads in the Python Forum
- Previous Thread: File transfering through Python
- Next Thread: Need Python Help
Views: 3843 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






