On 19/02/15 21:19, Chris Stinemetz wrote:
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.
I'm not sure what the difference is here? If you update the dictionary
or add a new key it will automatically have that key going forward.
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.
I'm not sure what 'band' is? You have not mentioned it up until now.
Below is what I have attempted thus far. I can provide sample data if
needed.
I think a short same of your data would be useful, both input and output.
import datetime
import string
> import pprint
You don't use string. Do you really need it? It's quite rarely
used these days.
And you don't use pprint either. Do you plan on having a use
for it later?
from datetime import datetime
# Open a files for reading
inFileOne = open('PRB_utilization.txt', "r")
You should get used to using the 'with' construct for file
handling, it's usually shorter and much more reliable.
iDas = "DB"
oDas = "D"
suffix = (iDas,oDas)
suffix = ("DB","D")
would be shorter and more obvious, since you don't use the
single value variables anywhere in the code..
dict = {'Cell': '','7':'','8':'','9':'','2':''}
Using dict as a name is a bad idea because it hides the
built in dict type conversion function.
for line in inFileOne.readlines():
You don't need readlines() any more you can just
do:
for line in inFileOne:
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
I'll assume that's all OK since we can't see the data format.
if site in dict:
dict['Cell'] = site
dict[band] = dl_prb_utl
else:
dict['Cell'] = site
dict[band] = dl_prb_utl
Both 'if' and 'else' clauses do the same thing so there
is no point in doing a test. Just use:
dict['Cell'] = site
dict[band] = dl_prb_utl
inFileOne.close();
You don't need this if you use the with... form.
Other than the minor tweaks I've suggested I'm not sure what your
problem is? I think we need to see the data to understand the issue.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor