Ask your own question, for FREE!
Mathematics 20 Online
OpenStudy (anonymous):

can anyone over here help me with python programming? im trying to write a code to read a txt file

OpenStudy (bibby):

What do you have written so far? ``` fo = open("foo.txt", "wb") ```

OpenStudy (anonymous):

well i have been given a skeleton file

OpenStudy (anonymous):

implement the function read_graph_from_file. The function is called with one argument (the name of a file), and must return a tuple with two elements, like this: (neighbour_list, position_list) = read_graph_from_file(filename) The first element in the tuple, neighbour_list, is the internal representation of the graph's structure. (This is defined below.) The second element is a list of node positions. This is only used for drawing the graph on screen.

OpenStudy (anonymous):

this is what im ment to be doing

OpenStudy (anonymous):

so far i have written: def read_graph_from_file(filename): my_file=open(filename,"rU") position_list=[] for row in my_file: value=row[:-1] my_list=value.split(",") try: if my_list[2] != "": nodenumber = my_list[0] positionX = my_list[1] positionY = my_list[2] position_list = tuple([positionX,positionY]) except IndexError: pass return([position_list]) (neighbour_list, position_list) = read_graph_from_file(filename)

OpenStudy (anonymous):

the code i have written so far is to find the position of the mode on the graph

OpenStudy (anonymous):

The format of the graph files is similar to the CSV files we have seen before, but different in that it contains two types of lines. For a graph with N nodes and M links, the first N lines have the form nodeNumber, positionX, positionY The nodes are numbered consecutively from 0 to N-1. The X and Y positions are integers. After this, the next M lines specify the links. They have the form nodeNumber1,nodeNumber2

OpenStudy (bibby):

tbh I know no python and prefer c++/java. I doubt I'll be of much help

OpenStudy (bibby):

Yeah I have no idea what I'm reading. Check out the computer science section

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!