0

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?

John
  • 15
  • 2
  • Possible duplicate of [How to write a dictionary into an existing file?](https://stackoverflow.com/questions/20169493/how-to-write-a-dictionary-into-an-existing-file) – John Apr 09 '19 at 13:58

3 Answers3

1

It looks like you are using the $ to split your line. That means it is no longer part of your line. I would recommend using one of the whitespace identifiers instead (whichever is appropriate to your text file).

def extractData(infile) :
    record = {}
    line = infile.readline()
    if line != "" :
        fields = line.split(' ') # or '\t'
        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)
John
  • 15
  • 2
Stephopolis
  • 1,765
  • 9
  • 36
  • 65
0

you are not associating the key with the value for all cases, in each iteration the dictionary is deleted, So when you want to access a value you will only have the last

I recommend this

def extractData(infile) :
    record = {}
    while True:
        line = infile.readline()
        if line == "" :
            break
        fields = line.split('$')
        record[fields[0]] = int(fields[1].replace(',', ""))
   return record


infile = open("percapita.txt", "r")
record = extractData(infile)
for k in record.keys():
    print(k , "  $"  , record[k])
John
  • 15
  • 2
Luis Alejandro
  • 101
  • 1
  • 3
0

In addition to @Stephopolis 's answer i think you are not using dictionary to its purpose. Your key values should be country names. As an example you should add values like this:

record["IsleofMan"] = "$83100"

and when you want to get per capita value for a country you simply query it from the dictionary.

print(record["IsleofMan"])

would give the output of

$83100

to get user input you can use:

country_query = input()

so when we bring it all together

def extractData(infile) :
    record = {}
    line = infile.readline()
    if line != "" :
        fields = line.split(' ') # or '\t'
        record[fields[0]] = fields[1]

return record

infile = open("percapita.txt", "r")
record = extractData(infile)
country_query = input()
print(record["IsleofMan"])

Further reading Dictionaries Input

rpanai
  • 12,515
  • 2
  • 42
  • 64
can
  • 444
  • 6
  • 14