Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a text file into a dictionary

I have the following text file for demand in a network.

Origin  1
    1 :      0.0;     2 :    100.0;     3 :    100.0;     4 :    500.0;     5 :    200.0;
    6 :    300.0;     7 :    500.0;     8 :    800.0;     9 :    500.0;    10 :   1300.0;
   11 :    500.0;    12 :    200.0;    13 :    500.0;    14 :    300.0;    15 :    500.0;
   16 :    500.0;    17 :    400.0;    18 :    100.0;    19 :    300.0;    20 :    300.0;
   21 :    100.0;    22 :    400.0;    23 :    300.0;    24 :    100.0;

Origin  2
    1 :    100.0;     2 :      0.0;     3 :    100.0;     4 :    200.0;     5 :    100.0;
    6 :    400.0;     7 :    200.0;     8 :    400.0;     9 :    200.0;    10 :    600.0;
   11 :    200.0;    12 :    100.0;    13 :    300.0;    14 :    100.0;    15 :    100.0;
   16 :    400.0;    17 :    200.0;    18 :      0.0;    19 :    100.0;    20 :    100.0;
   21 :      0.0;    22 :    100.0;    23 :      0.0;    24 :      0.0;

Origin  3
    1 :    100.0;     2 :    100.0;     3 :      0.0;     4 :    200.0;     5 :    100.0;
    6 :    300.0;     7 :    100.0;     8 :    200.0;     9 :    100.0;    10 :    300.0;
   11 :    300.0;    12 :    200.0;    13 :    100.0;    14 :    100.0;    15 :    100.0;
   16 :    200.0;    17 :    100.0;    18 :      0.0;    19 :      0.0;    20 :      0.0;
   21 :      0.0;    22 :    100.0;    23 :    100.0;    24 :      0.0;

... records 4-23 elided ...

Origin  24
    1 :    100.0;     2 :      0.0;     3 :      0.0;     4 :    200.0;     5 :      0.0;
    6 :    100.0;     7 :    100.0;     8 :    200.0;     9 :    200.0;    10 :    800.0;
   11 :    600.0;    12 :    500.0;    13 :    700.0;    14 :    400.0;    15 :    400.0;
   16 :    300.0;    17 :    300.0;    18 :      0.0;    19 :    100.0;    20 :    400.0;
   21 :    500.0;    22 :   1100.0;    23 :    700.0;    24 :      0.0;

Now I need to create a dictionary, which should look something like:

{(1,1):0.0, (1,2):100.0, (1, 3):100.0, .......
 (2, 1):100.0, (2,2):0, ......}

Where the tuple elements e.g. (1, 2) represent the origin and destination, and the value represents the demand (which is 100.0 for the (1, 2) key).

I tried the following:

with open("trips.txt", "r") as f:
     line = f.readline()
     line = f.readline()
     ind = 0
     while len(line):
         line = line.strip(';')
         l = line.split()
         print l

         ind = ind + 1
         if(ind == 5):
             line = f.readline()
             line = f.readline()
             line = f.readline()
             ind = 0
             node = node + 1
         else:
             line = f.readline()

But I don't think I am going anywhere with this...

like image 538
Nasif Avatar asked Mar 08 '23 22:03

Nasif


1 Answers

You're definitely not going anywhere, as you've made no reference at all to a dictionary.

I'll outline a process for you here; can you fill in the details?

my_dict = {}

while not EOF:
    # read the "Origin" line
    line = f.readline()

    # extract the number on the right
    origin_num = int( line.split()[-1] )

    # Read the data lines
    for _ in range(5):    # each data chunk has 5 lines
        data_line = readline()
        entries = data_line.split(';')    # split at semicolons

        for field in entries:
            y_key, value = field.split(:)
            # Now, you need to convert both of those to integers,
            #    combine v_key with the origin_num,
            #    and insert that value into my_dict.

Does that get you moving? Note that you also need to handle blank lines, detect end of file, etc.

like image 66
Prune Avatar answered Mar 21 '23 03:03

Prune