def generate_pajek(G):
    if G.name == '':
        name = 'NetworkX'
    else:
        name = G.name
    # Apparently many Pajek format readers can't process this line
    # So we'll leave it out for now.
    # yield '*network %s'%name

    # write nodes with attributes
    yield '*vertices %s' % (G.order())
    nodes = list(G)
    # make dictionary mapping nodes to integers
    nodenumber = dict(zip(nodes, range(1, len(nodes) + 1)))
    for n in nodes:
        na = G.node.get(n, {})
        x = na.get('x', 0.0)
        y = na.get('y', 0.0)
        id = int(na.get('id', nodenumber[n]))
        nodenumber[n] = id
        shape = na.get('shape', 'ellipse')
        s = ' '.join(map(make_qstr, (id, n, x, y, shape)))
        for k, v in na.items():
            if v.strip() != '':
                s += ' %s %s' % (make_qstr(k), make_qstr(v))
        yield s

    # write edges with attributes
    if G.is_directed():
        yield '*arcs'
    else:
        yield '*edges'
    for u, v, edgedata in G.edges(data=True):
        d = edgedata.copy()
        value = d.pop('weight', 1.0)  # use 1 as default edge value
        s = ' '.join(map(make_qstr, (nodenumber[u], nodenumber[v], value)))
        for k, v in d.items():
            if v.strip() != '':
                s += ' %s %s' % (make_qstr(k), make_qstr(v))
        yield s
Generate lines in Pajek graph format.
dataset/ETHPy150Open networkx/networkx/networkx/readwrite/pajek.py/generate_pajek