[Tutor] updating a dictionary

Chris Stinemetz chrisstinemetz at gmail.com
Thu Feb 19 22:19:14 CET 2015


Hello List,

I have a dictionary that I would like to update/add rows to it as I read a
file line by line.

The dictionary format looks like:

format = {'Cell': '','7':'','8':'','9':'','2':''}

For each line read in I would simply like to check to see if a Cell
key;value exists and if it does update the correct key==band(7,8,9,2)
within the dictionary.

If the Cell doesn't exist do the same thing as above only make sure to
update the Cell key:value with it's value form the file so it can check to
see if it exists later. There are duplicate Cell:values in the file so when
there is a duplicate it will need to look at band to see what key:value to
update.

Below is what I have attempted thus far. I can provide sample data if
needed.

Thank you in advance.

import datetime
import string
import pprint
from datetime import datetime

# Open a files for reading
inFileOne = open('PRB_utilization.txt', "r")

iDas = "DB"
oDas = "D"

suffix = (iDas,oDas)

dict = {'Cell': '','7':'','8':'','9':'','2':''}
for line in inFileOne.readlines():
    index = line.rstrip("\n").split("\t")

    cell = index[1]

    if cell.endswith(suffix, 14, 16) is False:
        eNb = cell[0:8]
        sector = cell[10:11]
        band = cell[9:10]
        dl_prb_utl = index[60]
        site = eNb + "_" + sector

        if site in dict:
            dict['Cell'] = site
            dict[band] = dl_prb_utl
        else:
            dict['Cell'] = site
            dict[band] = dl_prb_utl

inFileOne.close();


More information about the Tutor mailing list