You are getting that exception because node
is a string, but you are trying to access elements in the node
object as if it were a dictionary. To check the type of an object in Python, you can pass an object to the built in type()
function
>>> from networkx import nx
>>> G = nx.Graph()
>>> G.add_node("span")
>>> for node in G:
print node
print type(node)
"span"
<type 'str'>
So you don't want to iterate over G, instead you probably want to access nodes via their key from the graph object directly.
>>> G['spam']
{}
To explain why you got this particular exception - in Python you can access each character in a string via its index. For example
>>> node = "hello"
>>> node[0]
"h"
>>> node[4]
"o"
If you pass any other object, for example a string, you'll get a TypeError.
>>> node["x"]
TypeError: string indices must be integers, not str