Danny91
Danny91

Reputation: 21

Importing a File to python and then using it

Using this to work out a shortest path

What i'm trying to do is rather than have the nodes and distances in the program is to have them in a text file and have it load them rather than the ones in the program.

 # Use Graph()
    g = Graph()
    #Define a new array
    data = []
    # Open the import.txt file and import it into the array removing black space and splitting at commas.
    with open('import.txt') as inputfile:
        for line in inputfile:
            data.append(line.strip().split(','))

Is what i have so far however for the life of me cannot fine how to then take this data in data[] and put it into the graph so it can be used for the algorithm. Any direction would be appreciated!

In the code it is listed as this;

g = Graph()

g.add_vertex('a')
g.add_vertex('b')
g.add_vertex('c')
g.add_vertex('d')
g.add_vertex('e')
g.add_vertex('f')

g.add_edge('a', 'b', 7)  
g.add_edge('a', 'c', 9)
g.add_edge('a', 'f', 14)
g.add_edge('b', 'c', 10)
g.add_edge('b', 'd', 15)
g.add_edge('c', 'd', 11)
g.add_edge('c', 'f', 2)
g.add_edge('d', 'e', 6)
g.add_edge('e', 'f', 9)

However i would like to put this in a text file so it can be changed easily. An example of the text file id like to use is;

'a', 'e', 2
'a', 'b', 5
'b', 'c', 9
'e', 'c', 8
'e', 'd', 7
'd', 'c', 1

Upvotes: 2

Views: 81

Answers (2)

Adam Smith
Adam Smith

Reputation: 54203

This is really just refining Emerson Cardoso's excellent answer. Since you're creating a comma-separated values file, you should use the csv module to parse it. Also I don't see a big need to create a list of the vertices/edges before parsing them -- all you should need is a set of the vertex names so you can create any vertices you haven't seen yet.

# filename.csv
a, e, 2
a, b, 5
b, c, 9
e, c, 8
e, d, 7
d, c, 1

 

# yourscript.py
import csv

g = Graph()
vertices = set()

with open("filename.csv") as csv_f:
    reader = csv.reader(csv_f)
    for line in reader:
        from_, to, distance = line
        if from_ not in vertices:
            g.add_vertex(from_)
            vertices.add(from_)
        if to not in vertices:
            g.add_vertex(to)
            vertices.add(to)
        g.add_edge(from_, to, distance)

Upvotes: 1

Emerson Cardoso
Emerson Cardoso

Reputation: 394

You could do something like this:

File: graph_info.txt

a, e, 2
a, b, 5
b, c, 9
e, c, 8
e, d, 7
d, c, 1

Code: your_code.py

content = ''
with open('graph_info.txt', 'r') as f:
    content = f.read()


vertex_dict = {}
edges_list = []

lines = content.split('\n')

for l in lines:
    edge_info = l.split(', ')
    edges_list.append(edge_info)
    if edge_info[0] not in vertex_dict:
        vertex_dict[edge_info[0]] = True
    if edge_info[1] not in vertex_dict:
        vertex_dict[edge_info[1]] = True

# populating graph information
g = Graph()

for vertex_name, dummy in vertex_dict.iteritems():
    g.add_vertex(vertex_name)

for edge_info in edges_list:
    g.add_edge(edge_info[0], edge_info[1], int(edge_info[2]))

Upvotes: 2

Related Questions