-1
import numpy as np
import re
def read_process_OD():
    OD = np.zeros([24, 24])
    lines=[]
    with open('Network_OD.txt','r') as f:
        lines=f.readlines()
    origin=[]
    for line in lines:
        if line.startswith('Origin')==True:
            pass
        else:
            origin.append(re.split(pattern=[':;'],string=lines))

#raw text is shown like this: #Origin 1

1 : 0.0; 2 : 100.0; 3 : 100.0; 4 : 500.0; 5 : ###200.0;

  • 1
    What is your question? –  Oct 15 '21 at 16:16
  • Hi brutus, I am trying to convert raw text as a matrix [24,24]. My text has titles, spaces, colons and semicolons. I don't know how to proceed. The lines are ordered by origin destination nodes. for instance: Origin 6: 1:100.0;2:400.0, and on. Origin is the row index, and the data following are the value of the columns. – Paloma Fernandez Oct 15 '21 at 16:33

1 Answers1

0

I think what you are trying to get from this text is what that is called a "dictionary" in python. So, let's get to the answer. First, you have to split the text by the ";"

splitted_text=raw_text.split(";")

you will have:

["1 : 0.0", "2 : 100.0", "3 : 100.0", "4 : 500.0", "5 :200.0"]

which is not a dictionary, yet. you might have guessed that the next step is splitting each element of the list above by ":". Here, to make the long story short, I will get to the final part:

my_dict={}
for i in splitted_text:
    dict[int(i.split(":")[0])]=float(i.split(":")[1])

And, by this, you will have a dictionary ready to be analyzed, make graph of , etc. :

{1:0.0,2:100.0,3:100.0,4:500.0,5:200.0}
Hamid Rajabi
  • 59
  • 1
  • 8
  • Thank, Hamid!! Can I convert the dictionary into an array after that? the key is the column index. – Paloma Fernandez Oct 15 '21 at 16:40
  • Dear Paloma, I believe you have a background in C or Java script. In Python there is no such a thing as "array". If you mean something like: [1,2,4,5]; this is called a list in python. – Hamid Rajabi Oct 16 '21 at 17:41