Repository: networkx/networkx

Issue:
Potential bug in networkx.write_gexf()
I'm using `2.0rc2.dev20170911220036` 
``` bash
=> pip freeze | grep networkx
networkx==2.0rc2.dev20170911220036
```
and when I try to write to gexf `networkx.write_gexf(G,filename)`, it gave me the following error:


```bash
Traceback (most recent call last):
  File "graph_nx2.py", line 434, in <module>
    verbose=True)
  File "graph_nx2.py", line 55, in __init__
    self.OutputGEXF()
  File "graph_nx2.py", line 422, in OutputGEXF
    nx.write_gexf(self.G, self.file_name)
  File "<decorator-gen-388>", line 2, in write_gexf
  File "/home/me/anaconda2/envs/nx2/lib/python2.7/site-packages/networkx-2.0rc2.dev20170911220036-py2.7.egg/networkx/utils/decorators.py", line 224, in _open_file
    result = func(*new_args, **kwargs)
  File "/home/me/anaconda2/envs/nx2/lib/python2.7/site-packages/networkx-2.0rc2.dev20170911220036-py2.7.egg/networkx/readwrite/gexf.py", line 88, in write_gexf
    writer.add_graph(G)
  File "/home/me/anaconda2/envs/nx2/lib/python2.7/site-packages/networkx-2.0rc2.dev20170911220036-py2.7.egg/networkx/readwrite/gexf.py", line 306, in add_graph
    self.add_edges(G, graph_element)
  File "/home/me/anaconda2/envs/nx2/lib/python2.7/site-packages/networkx-2.0rc2.dev20170911220036-py2.7.egg/networkx/readwrite/gexf.py", line 363, in add_edges
    for u, v, key, edge_data in edge_key_data(G):
  File "/home/me/anaconda2/envs/nx2/lib/python2.7/site-packages/networkx-2.0rc2.dev20170911220036-py2.7.egg/networkx/readwrite/gexf.py", line 349, in edge_key_data
    edge_data = data.copy()
AttributeError: 'int' object has no attribute 'copy'
```


After looking into the source code, I find this code block in `networkx.readwrite.gexf` for the `multiDiGraph` that I'm using:
```python
def add_edges(self, G, graph_element):
        def edge_key_data(G):
            # helper function to unify multigraph and graph edge iterator
            if G.is_multigraph():
                for u, v, data, key in G.edges(data=True, keys=True):
                    edge_data = data.copy()
                    edge_data.update(key=key)
                    edge_id = edge_data.pop('id', None)
                    if edge_id is None:
                        edge_id = next(self.edge_id)
                    yield u, v, edge_id, edge_data
```

Seems like the problem is `u, v, data, key in G.edges(data=True, keys=True)`, which I believe should instead be  `u, v, key, data in G.edges(data=True, keys=True)`, with `data` and `key` swapped.

Reason for that is when I get the following:
```python
>>> G.edges(data=True, keys=True)
>>> ... ('node0', 'node1', 0, {'variable': 'g', 'delay': 0.0}) ...
```

Could this be a bug? Or am I doing something wrong


Discussion:
This looks like a bug --  Thanks for the detailed report!
I think its just a matter of switching order, but I'll check to make sure. 

Resolving patch:
diff --git a/networkx/readwrite/gexf.py b/networkx/readwrite/gexf.py
--- a/networkx/readwrite/gexf.py
+++ b/networkx/readwrite/gexf.py
@@ -345,7 +345,7 @@ def add_edges(self, G, graph_element):
         def edge_key_data(G):
             # helper function to unify multigraph and graph edge iterator
             if G.is_multigraph():
-                for u, v, data, key in G.edges(data=True, keys=True):
+                for u, v, key, data in G.edges(data=True, keys=True):
                     edge_data = data.copy()
                     edge_data.update(key=key)
                     edge_id = edge_data.pop('id', None)


Test patch: