0

I have a file which looks like this:

ID  Name    Abbr    Disctrict
*data*
1   Newcastle   NC  AA,BB,CC
2   Manchester  MCR AA,DD,FF
3   Liverpool   LV  FF,GG,HH

I would like to have a dictionary which looks like this:

{'AA': 'NC', 'BB': 'NC', 'CC':'NC',
'AA': 'MCR', 'DD':'MCR': 'FF': 'MCR'
'FF': 'LV', 'GG': 'LV', 'HH': 'LV'}

So I tried this:

my_dict = {}

path = (r'c:\data\GG\Desktop\Extra\UK_test_cities.txt')

with open(path, 'r') as f:
    for line in f:
        (key, val) = line.split()
        my_dict[int(key)] = val

I got this:

  File "c:/data/GG/Desktop/Extra/test_1.py", line 17, in <module>
    (key, val) = line.split()
ValueError: too many values to unpack (expected 2)
TangerCity
  • 775
  • 2
  • 7
  • 13
  • Python dict key values are unique. It's not possible to have the same key twice with different values. Your test-case with `AA` either is going to be `NC` or `MCR`, not both. – Dschoni Mar 17 '21 at 08:47
  • I know that, its just an example. – TangerCity Mar 17 '21 at 08:53

3 Answers3

0

In a dict, if a key is an object, there are no duplicate problems.

my_dict = {}

with open("C:/Users/lenovo/Desktop/text.txt", 'r') as f:
    line_ = []
    for line in f:
        line_.append(line.split())

    dict_ = {str(j):line_[i][2] for i in range(2, 5) for j in line_[i][3].split(',')}
    print(dict_)

OUTPUT :

{'AA': 'MCR', 'BB': 'NC', 'CC': 'NC', 'DD': 'MCR', 'FF': 'LV', 'GG': 'LV', 'HH': 'LV'}      

But to get output as per your requirement then you have to can change the behavior of the built in types in Python.

Davinder Singh
  • 2,060
  • 2
  • 7
  • 22
-1
import collections

my_dict = collections.defaultdict(set)

path = (r'c:\data\GG\Desktop\Extra\UK_test_cities.txt')

with open(path, 'r') as f:
    f.next() # skip title
    for line in f:
        (ID, Name, Abbr, Disctrict) = line.split() # the 4 columns
        for unique_district in Disctrict.split(','):
            my_dict[unique_district].add(Abbr) ## multiple values for one key
  • This is not the same output as I desired – TangerCity Mar 17 '21 at 09:06
  • The thing is that the output you want is impossible: {'AA': 'NC', 'BB': 'NC', 'CC':'NC', 'AA': 'MCR', 'DD':'MCR': 'FF': 'MCR' , 'FF': 'LV', 'GG': 'LV', 'HH': 'LV'}. Keys are unique, so you can't have "AA" twice or more for example. So this way, you have "AA" once with multiple values. If you don't want this, then you could use a list instead of dict – GIOVANNI QUINONES VALDEZ Mar 17 '21 at 09:10
  • @GIOVANNIQUINONESVALDEZ please skip first two line using `next(f) in python3.x` or `f.next() lower vesion` – Davinder Singh Mar 17 '21 at 09:14
-1

There are several problems with your code. You can't insert multiple keys in a dictionary. Also, the text file was not well formated. there was naming and spacing problem.

You can try this.

import re

my_data = []
path = "input.txt"
with open(path, "r") as f:
    reg = "([0-9]+)[ ]+(\w+)[ ]+(\w+)[ ]+([\w+,]+)"
    for line in f.readlines():
        reobj = re.search(reg, line)
        value, keys = reobj.group(3), reobj.group(4)
        temp_keys = keys.split(",")
        my_data.append(dict(zip(temp_keys, [value] * len(temp_keys))))
# Outpus was
# [{'AA': 'NC', 'BB': 'NC', 'CC': 'NC'}, {'AA': 'MCR', 'DD': 'MCR', 'FF': 'MCR'}, {'FF': 'LV', 'GG': 'LV', 'HH': 'LV'}]

Nayan Biswas
  • 112
  • 1
  • 4