I am trying to store a text file, which contains country names and their per capita income, into a dictionary, and output the dictionary.
The code that I have so far stores the file in the dictionary and outputs it successfully, except I could not figure out how to do so without removing the dollar signs from the values.
This is my code so far:
def extractData(infile) :
record = {}
line = infile.readline()
if line != "" :
fields = line.split('$')
record["country"] = fields[0]
record["percap"] = int(fields[1].replace(',', ""))
return record
infile = open("percapita.txt", "r")
record = extractData(infile)
while len(record) > 0:
print("%-20s %10d" % (record["country"], record["percap"]))
record = extractData(infile)
How can I fix this?