problem with dictionaries

Steve Holden steve at holdenweb.com
Wed Apr 23 08:09:22 EDT 2008


Simon Strobl wrote:
> Hello,
> 
> the idea of the following program is to parse a frequency list of the
> form FREQUENCY|WORD, to store the frequency of a word in a dictionary
> (and to do some things with this information later).
> 
> I have done this many many times. Suddenly, it does not work any more:
> The value frq[key] is different from the value that key has in the
> file 'my_frqlist.txt'.
> 
> I am using Python 2.5.1
> 
> Any hints?
> 
> Simon
> 
> ================================================
> 
> #!/usr/bin/python
> 
> import sys
> 
> frqlist = open('my_frqlist.txt', 'r')
> 
> # my_frqlist looks like this:
> # 787560608|the
> # 434879575|of
> # 413442185|and
> # 395209748|to
> # 284833918|a
> # 249111541|in
> # 169988976|is
> 
> frq = {}
> 
> for line in frqlist:
>     line = line.rstrip()
>     frequency, word = line.split('|')
>     frq[word] = int(frequency)
> 
> for key in frq.keys():
>     print key,  frq[key]
> 
<flippancy>You musts have missed the memo. The rules of the universe 
changed at 0834 UST yesterday, and all functioning Python programs 
stopped working.</flippancy>

More seriously, *something* must have changed - it's probably not the 
rules of the universe though. Are the files now coming from a different 
source (Windows rather than Unix or vice versa)?

As you read in the data, insert a

     print "%r: %s %s" % (line, frequency, word)

to see exactly what is being processed and how it is getting split.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list