Multiple files reading
MRAB
python at mrabarnett.plus.com
Tue Jun 14 16:27:26 EDT 2016
On 2016-06-14 21:06, Joaquin Alzola wrote:
> >> The dictionary that I am using in the classes:
> >> {'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3',
> >> 'DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0
> >> ,'5012':0}}
> >>
> >> Wanted help from your side on how to focus this just because I
want to read the files once (not 6 times) and then use classes to get
back the dictionary value ...
> >>
> >> I need just advice on steps to follow...
> >>
> >Use a dict (or defaultdict) where the key is the country and the
value is info (class?) related to that country.
>
> Putting a dict for each country such as:
> self.dictionaryNL =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
> self.dictionarySP =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
> self.dictionaryUK =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
> self.dictionaryFR =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
> self.dictionaryDK =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
> self.dictionaryGR =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
>
> makes the trick but I have to repeat 6 time all the time. Now with
that config I read only once the files saving 125k read lines.
>
> But as always I think it can be done better (just started to learn
python about 8 month ago). Thanks for the tips.
>
I think that what you're doing currently is scanning the files multiple
times, once for each country, looking in the appropriate field for the
country to see whether you should read or skip.
What I meant was that you would have a dict of dicts, where the key was
the country:
self.dictionary = {}
for country in ('NL', 'SP', 'UK', 'FR', 'DK', 'GR'):
self.dictionary[country] =
{'Country':'Empty','Service':'Empty','TimeStamp':'Empty','Ocg':'see3','DiameterCodes':{'2001':0,'4010':0,'4012':0,'4998':0,'4999':0,'5007':0,'5012':0}}
You could then scan the files _once_, looking in the appropriate field
for the country, and then updating the appropriate dictionary.
More information about the Python-list
mailing list