[Tutor] iterating data and populating a dictionary

Monika Jisswel monjissvel at googlemail.com
Tue Aug 5 17:37:34 CEST 2008


maybe this can help

MainDictionary = {}
N = 0
for line in open('data_file', 'r'):
    if line:
        N += 1
        a, b = line.split(" = ")  # "=" is in between spaces whish gives you
only two variables.
        if a == 'Field':
            MainDictionary[N] == {}
        elif a == 'Index':
            MainDictionary[N]['Value'] = {b}



2008/8/5 Bryan Fodness <bryan.fodness at gmail.com>

> i am filling a dictionary with a dictionary and my values for
> isegment[field] are identical.  i can't see where i am overwriting the
> previous field values.
>
> my data looks like
>
> Field = aa1
> Index = 0.0
> Value = 0.0
> ...
> ...
>  Field = aa2
> Index = 0.01
> Value = 0.5
> ...
>
>
> i would like to have something like,  {1: {'Value': 0.0, ...}, 2: {'Value':
> 0.01, ...}}
>
> for line in file(data_file):
>     the_line = line.split()
>     if the_line:
>         if the_line[0] == 'Field':
>             field += 1
>         elif the_line[0] == 'Index':
>             index = float(the_line[-1])
>             dif_index = index - start_index
>             iindex[field] = dif_index
>             start_index = index
>         elif the_line[0] == 'Value':
>             segment[the_line[1]] = float(the_line[-1])*(ax/40)
>             isegment[field] = segment
>
>
> --
> "The game of science can accurately be described as a never-ending insult
> to human intelligence." - João Magueijo
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080805/f4308401/attachment.htm>


More information about the Tutor mailing list