Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi. I am trying to figure out how to implement a method I created in a function . The method is : def setPopulationOfCountry (self,

Hi. I am trying to figure out how to implement a method I created in a function . The method is :

def setPopulationOfCountry (self, country, population): country = country.title() if country in self._countryCat.keys(): self._countryCat[country].setPopulation(population) return True else: return False 

and is held within the class country.

This is the code I am trying to figure out

def processUpdates (cntryFileName, updateFileName): updateFile = open(updateFileName, "r") lines = updateFile.readlines() for line in lines: line = line.replace(",", "") line = line.replace(" ", "") content = line.rsplit(";") #print(content) for i in content: if "P" in str(content[i]): content[i].setPopulationOfCountry(str(content[0]), int(content[i])) print(i) updateFile.close() cntryFileName = open(cntryFileName, "r") cntryData = cntryFileName.read() print(cntryData) cntryFileName.close() processUpdates("data.txt", "upd.txt") 

Basically if the list (content) contains a P=(a number) I want to add the number to the existing file using method setPopulation .

Here is the entire code incase there is an error elsewhere causing my problem :

from country import Country class CountryCatalouge: def __init__(self, data): self._data = open(data, "r") self._cDictionary = {} self._countryCat = {} self._lines = self._data.readlines()[1:] #skipping the first line with header for line in self._lines: line = line.split("|") line[2] = line[2].replace(",", "") line[3] = line[3].replace(",", "") self._cDictionary[line[0]]= line[1] self._countryCat[line[0]] = Country(line[0], int(line[2]), float(line[2].strip()), self._cDictionary[line[0]]) self._data.close() def __repr__ (self): s = '' for country in self._countryCat.values(): s += country.__repr__() + ' ' return s def setPopulationOfCountry (self, country, population): country = country.title() if country in self._countryCat.keys(): self._countryCat[country].setPopulation(population) return True else: return False def setAreaOfCountry (self, country, area): country = country.title() if country in self._countryCat.keys(): self._countryCat[country].setArea(area) return True else: return False def findCountry (self, country): if country.title() in self._countryCat.keys(): result = self._countryCat[country] return result else: return None def addCountry (self, country, population, area, continent): country = country.title() if country not in self._cDictionary.keys(): self.countryCat[country] = Country(country, int(population), int(area), continent) return True else: return False def printCountryCatalouge (self): for country in self._countryCat.keys(): print(self._countryCat[country]) def saveCountryCatalouge(self, fname): file_object = open (fname, "w") countryList = [] for country in self._countryCat.keys(): countryList.append(country) countryList = sorted(countryList) count = 0 for country in countryList: countryName = self._countryCat[country].getName() population = self._countryCat[country].getPopulation() area = self._countryCat[country].getArea() continent = self._countryCat[country].getContinent() file_object.write(countryName + "|" + str(population) + "|" + str(area) +"|" +" ") count = count + 1 file_object.close() return count def processUpdates (cntryFileName, updateFileName): updateFile = open(updateFileName, "r") lines = updateFile.readlines() for line in lines: line = line.replace(",", "") line = line.replace(" ", "") content = line.rsplit(";") #print(content) for i in content: if "P" in str(content[i]): content[i].setPopulationOfCountry(str(content[0]), int(content[i])) print(i) updateFile.close() cntryFileName = open(cntryFileName, "r") cntryData = cntryFileName.read() print(cntryData) cntryFileName.close() processUpdates("data.txt", "upd.txt") 

Here is the data.txt

Country|Continent|Population|Area Brazil|South America|193,364,000|8,511,965 Canada|North America|34,207,000|9,976,140 China|Asia|1,339,190,000|9,596,960 Colombia|South America|48,654,392|1,090,909 Egypt|Africa|93,383,574|1,000,000 France|Europe|64,668,129|541,656 Indonesia|Asia|260,581,100|1,809,590 Italy|Europe|59,801,004|300,000 Japan|Asia|127,380,000|377,835 Mexico|North America|128,632,004|1,969,230 Nigeria|Africa|186,987,563|912,134 South Africa|Africa|54,978,907|1,222,222 South Korea|Asia|50,503,933|98,076 United States of America|North America|309,975,000|9,629,091 

Here is upd.txt

Brazil;P=193,364,111;A=8,511,966 Indonesia;P=260,581,345 Italy;P=59,801,999 Japan;P=127,380,555;A=377,800 Sweden;P=9,995,345;A=450,295;C=Europe Vietnam;A=331,310;P=95,541,921;C=Asia Mexico;P=128,632,777 Colombia;A=1,091,111;P=48,654,404 Egypt;P=93,384,001 France;P=64,669,829 

And Here is Country.py

class Country: def __init__(self,name,pop,area,continent): self._name = str(name) self._name = name self._pop = pop self._area = area self._continent = continent def setPop(self, pop): self._pop = int(pop) def setArea (self,area): self._area = int(area) def setContinent(self, continent): self._continent = str(continent) def getName (self): return self._name def getPopulation (self): return self._pop def getArea (self): return self._area def getContinent (self): return self._continent def __repr__(self): return self._name + " (pop: " + str(self._pop) + ", size: " + str(self._area) + ") in " + self._continent 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions