Qwert
Qwert

Reputation: 67

How to add mutiple values in a dictionary from file in python

Given a file data.txt: with a list of country name and their population and area, the file looks like following:

China|1,339,190,000|9,596,960.00
Brazil|193,364,000|8,511,965.00
Japan|127,380,000|377,835.00
Canada|34,207,000|9,976,140.00
Indonesia|260,581,100|1,809,590.97

I wanted to create a dictionary with the country name(key) and two values(population and area).

and the final output should be something like this:

China:[1339190000,9596960.00]

Where the population is an intger and the area is a float.

Here is my code, I don't know what Im doing it wrong, I was wondering if some one can point it out for me.

Thanks.

Here is my code:

Country = {}
file = open("data.txt", "r")
for i in file :
  file1 = i.strip(",") 
  parts = i.split("|")
  length = len(file[i])
  if length in parts:
    Country[length][i] = file[i]
  else:
    Country[length] = {i: file[i]}
  print(parts)

Upvotes: 1

Views: 73

Answers (3)

Anton vBR
Anton vBR

Reputation: 18916

You can use pandas, read_csv and use the thousands separator.

import pandas as pd

cols = ['Country','Pop','Area']
df = pd.read_csv(filename,sep="|",thousands=r',',header=None,names=cols,index_col=cols[0])
d = {t[0]:t[1:] for t in df.itertuples()}

d is now a dict:

{'Brazil': (193364000, 8511965.0),
 'Canada': (34207000, 9976140.0),
 'China': (1339190000, 9596960.0),
 'Indonesia': (260581100, 1809590.97),
 'Japan': (127380000, 377835.0)}

old code

d = df.to_dict('i') # returns float instead of int
d = {t[0]:dict(zip(df.columns,t[1:])) for t in df.itertuples()} # use this instead

{'Brazil': {'Area': 8511965.0, 'Pop': 193364000},
 'Canada': {'Area': 9976140.0, 'Pop': 34207000},
 'China': {'Area': 9596960.0, 'Pop': 1339190000},
 'Indonesia': {'Area': 1809590.97, 'Pop': 260581100},
 'Japan': {'Area': 377835.0, 'Pop': 127380000}}

Upvotes: 2

Daniel
Daniel

Reputation: 607

You can also write a simple function that takes a file for input and returns a dictionary with your data. There are a lot of different ways you could solve this problem, the only limit is creativity. Hope this helps :)

def text_to_dict(filename):

    with open(filename, 'r') as file:

        data = file.read()
        line = data.split('\n')

        result = {}

        for item in line:
            item_list = item.split('|')
            result[item_list[0]] = item_list[1], item_list[2]

        return result

print(text_to_dict('data.txt'))  # Calling our function

# Output: {'China': ('1,339,190,000', '9,596,960.00'), 'Brazil': ('193,364,000', '8,511,965.00'), 'Japan': ('127,380,000', '377,835.00'), 'Canada': ('34,207,000', '9,976,140.00'), 'Indonesia': ('260,581,100', '1,809,590.97')}

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71461

{China:1339190000:9596960.00} is invalid syntax; however, you can try this:

file_data = [i.strip('\n').split('|') for i in open('filename.txt')]
final_data = {i[0]:[c for c in map(float, [''.join(b.split(',')) for b in i[1:]])] for i in file_data}

Output:

{'Japan': [127380000.0, 377835.0], 'Canada': [34207000.0, 9976140.0], 'Brazil': [193364000.0, 8511965.0], 'Indonesia': [260581100.0, 1809590.97], 'China': [1339190000.0, 9596960.0]}

Upvotes: 1

Related Questions