import sys USAGE = "USAGE: python convert-matrix-format.py " # Parse the command line. if (len(sys.argv) != 2): print USAGE sys.exit(1) inFileName=sys.argv[1] # Open the file. inFile=open (inFileName,'r') # Initialize dictionaries for node list and network. myNet={} nodeNames = {} # Read the file. for line in inFile: line =line.rstrip() words=line.split() # Check for a valid entry. if (len(words)!=2): print "Invalid line (%s)\n" %line sys.exit(1) # Store the node names and the network. nodeNames[words[0]] = 1 nodeNames[words[1]] = 1 myNet[(words[0],words[1])]=1 inFile.close() print"Found %d nodes and %d edges.\n" %(len(nodeNames),len(myNet)) # Print the first line. for nodeName in nodeNames.keys(): sys.stdout.write("\t%s" % nodeName) sys.stdout.write("\n") # Print subsequent rows. for nodeName1 in nodeNames.keys(): sys.stdout.write(nodeName1) for nodeName2 in nodeNames.keys(): # Test whether this edge exists. if (myNet.has_key((nodeName1, nodeName2))): sys.stdout.write("\t1") else: sys.stdout.write("\t0") sys.stdout.write("\n")