write_pajek#

write_pajek(G, path, encoding='UTF-8')[source]#

Write graph in Pajek format to path.

Parameters:
Ggraph

A Networkx graph

pathfile or string

File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

Warning

Optional node attributes and edge attributes must be non-empty strings. Otherwise it will not be written into the file. You will need to convert those attributes to strings if you want to keep them.

References

See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm for format information.

Examples

>>> from pathlib import Path
>>> import tempfile
>>> tmp_path = Path(tempfile.gettempdir())
>>> fpath = tmp_path / "test.net"
>>> G = nx.path_graph(4)
>>> nx.write_pajek(G, fpath)
>>> with open(fpath) as fh:  # The data as stored in pajek format
...     print(fh.read())
*vertices 4
1 0 0.0 0.0 ellipse
2 1 0.0 0.0 ellipse
3 2 0.0 0.0 ellipse
4 3 0.0 0.0 ellipse
*edges
1 2 1.0
2 3 1.0
3 4 1.0