本文整理汇总了Python中networkx.utils._get_fh函数的典型用法代码示例。如果您正苦于以下问题:Python _get_fh函数的具体用法?Python _get_fh怎么用?Python _get_fh使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_get_fh函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_graphml
def write_graphml(G, path, encoding='utf-8'):
"""Write G in GraphML XML format to path
Parameters
----------
G : graph
A networkx graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_graphml(G, "test.graphml")
Notes
-----
This implementation does not support mixed graphs (directed and unidirected
edges together) hyperedges, nested graphs, or ports.
"""
fh = _get_fh(path, mode='wb')
writer = GraphMLWriter(encoding=encoding)
writer.add_graph_element(G)
writer.dump(fh)
示例2: write_d3_js
def write_d3_js(G, path, group=None, encoding="utf-8"):
"""Writes a NetworkX graph in D3.js JSON graph format to disk.
Parameters
----------
G : graph
a NetworkX graph
path : file or string
File or filename to write. If a file is provided, it must be
opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed.
group : string, optional
The name 'group' key for each node in the graph. This is used to
assign nodes to exclusive partitions, and for node coloring if visualizing.
encoding: string, optional
Specify which encoding to use when writing file.
Examples
--------
>>> from networkx.readwrite import d3_js
>>> G = nx.path_graph(4)
>>> G.add_nodes_from(map(lambda i: (i, {'group': i}), G.nodes()))
>>> d3_js.write_d3_js(G, 'four_color_line.json')
"""
fh = _get_fh(path, 'wb')
graph_json = d3_json(G, group)
graph_dump = json.dumps(graph_json, indent=2)
fh.write(graph_dump.encode(encoding))
示例3: read_graphml
def read_graphml(path,node_type=str):
"""Read graph in GraphML format from path.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
Returns
-------
graph: NetworkX graph
If no parallel edges are found a Graph or DiGraph is returned.
Otherwise a MultiGraph or MultiDiGraph is returned.
Notes
-----
This implementation does not support mixed graphs (directed and unidirected
edges together), hypergraphs, nested graphs, or ports.
"""
fh=_get_fh(path,mode='rb')
reader = GraphMLReader(node_type=node_type)
# need to check for multiple graphs
glist=list(reader(fh))
return glist[0]
示例4: read_leda
def read_leda(path, encoding='UTF-8'):
"""Read graph in LEDA format from path.
Parameters
----------
path : file or string
File or filename to read. Filenames ending in .gz or .bz2 will be
uncompressed.
Returns
-------
G : NetworkX graph
Examples
--------
G=nx.read_leda('file.leda')
References
----------
.. [1] http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html
"""
fh=_get_fh(path,mode='rb')
lines=(line.decode(encoding) for line in fh)
G=parse_leda(lines)
fh.close()
return G
示例5: read_gpickle
def read_gpickle(path):
"""Read graph object in Python pickle format.
Pickles are a serialized byte stream of a Python object [1]_.
This format will preserve Python objects used as nodes or edges.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be uncompressed.
Returns
-------
G : graph
A NetworkX graph
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_gpickle(G,"test.gpickle")
>>> G=nx.read_gpickle("test.gpickle")
References
----------
.. [1] http://docs.python.org/library/pickle.html
"""
fh=_get_fh(path,'rb')
return pickle.load(fh)
示例6: write_graphml
def write_graphml(G, path, encoding='utf-8',prettyprint=True):
"""Write G in GraphML XML format to path
Parameters
----------
G : graph
A networkx graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
encoding : string (optional)
Encoding for text data.
prettyprint : bool (optional)
If True use line breaks and indenting in output XML.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_graphml(G, "test.graphml")
Notes
-----
This implementation does not support mixed graphs (directed and unidirected
edges together) hyperedges, nested graphs, or ports.
"""
fh = _get_fh(path, mode='wb')
writer = GraphMLWriter(encoding=encoding,prettyprint=prettyprint)
writer.add_graph_element(G)
writer.dump(fh)
示例7: read_dimacs
def read_dimacs(path):
"""Read graph in DIMACS format from path.
Parameters
----------
path : string or file
Filename or file handle to read.
Returns
-------
G: NetworkX graph
The graph corresponding to the lines in DIMACS format.
Examples
--------
>>> G=nx.DiGraph()
>>> G.add_edge('s', 'b', capacity = 2)
>>> G.add_edge('s', 'c', capacity = 1)
>>> G.add_edge('c', 'd', capacity = 1)
>>> G.add_edge('d', 'a', capacity = 1)
>>> G.add_edge('b', 'a', capacity = 2)
>>> G.add_edge('a', 't', capacity = 2)
>>> nx.write_dimacs(G, 's', 't', "test.dimacs" )
>>> [G,s,t]=nx.read_dimacs("test.dimacs")
See Also
--------
write_dimacs
"""
fh = _get_fh(path, mode='r')
lines = (line for line in fh)
return parse_dimacs(lines)
示例8: read_pajek
def read_pajek(path,encoding='UTF-8'):
"""Read graph in Pajek format from path.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be uncompressed.
Returns
-------
G : NetworkX MultiGraph or MultiDiGraph.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_pajek(G, "test.net")
>>> G=nx.read_pajek("test.net")
To create a Graph instead of a MultiGraph use
>>> G1=nx.Graph(G)
References
----------
See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
for format information.
"""
fh=_get_fh(path, 'rb')
lines = (line.decode(encoding) for line in fh)
return parse_pajek(lines)
示例9: write_pajek
def write_pajek(G, path, encoding='UTF-8'):
"""Write graph in Pajek format to path.
Parameters
----------
G : graph
A Networkx graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_pajek(G, "test.net")
References
----------
See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
for format information.
"""
fh=_get_fh(path, 'wb')
for line in generate_pajek(G):
line+='\n'
fh.write(line.encode(encoding))
示例10: write_gml
def write_gml(G, path):
"""
Write the graph G in GML format to the file or file handle path.
Parameters
----------
path : filename or filehandle
The filename or filehandle to write. Filenames ending in
.gz or .gz2 will be compressed.
See Also
--------
read_gml, parse_gml
Notes
-----
GML specifications indicate that the file should only use
7bit ASCII text encoding.iso8859-1 (latin-1).
For nested attributes for graphs, nodes, and edges you should
use dicts for the value of the attribute.
Examples
---------
>>> G=nx.path_graph(4)
>>> nx.write_gml(G,"test.gml")
Filenames ending in .gz or .bz2 will be compressed.
>>> nx.write_gml(G,"test.gml.gz")
"""
fh=_get_fh(path,mode='wb')
for line in generate_gml(G):
line+='\n'
fh.write(line.encode('latin-1'))
示例11: read_graphml
def read_graphml(path, node_type=str):
"""Read graph in GraphML format from path.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
Returns
-------
graph: NetworkX graph
If no parallel edges are found a Graph or DiGraph is returned.
Otherwise a MultiGraph or MultiDiGraph is returned.
Notes
-----
This implementation does not support mixed graphs (directed and unidirected
edges together), hypergraphs, nested graphs, or ports.
Files with the yEd "yfiles" extension will can be read but the graphics
information is discarded.
yEd compressed files ("file.graphmlz" extension) can be read by renaming
the file to "file.graphml.gz".
"""
fh = _get_fh(path, mode='rb')
reader = GraphMLReader(fh, node_type)
return reader()[0]
示例12: write_gpickle
def write_gpickle(G, path):
"""Write graph in Python pickle format.
Pickles are a serialized byte stream of a Python object [1]_.
This format will preserve Python objects used as nodes or edges.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_gpickle(G,"test.gpickle")
References
----------
.. [1] http://docs.python.org/library/pickle.html
"""
fh=_get_fh(path,mode='wb')
pickle.dump(G,fh,pickle.HIGHEST_PROTOCOL)
fh.close()
示例13: read_pajek
def read_pajek(path):
"""Read graph in Pajek format from path.
Returns a MultiGraph or MultiDiGraph.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_pajek(G, "test.net")
>>> G=nx.read_pajek("test.net")
To create a Graph instead of a MultiGraph use
>>> G1=nx.Graph(G)
"""
fh=_get_fh(path,mode='r')
G=parse_pajek(fh)
return G
示例14: read_dot
def read_dot(path):
"""Return a NetworkX MultiGraph or MultiDiGraph from a dot file on path.
Parameters
----------
path : filename or file handle
Returns
-------
G : NetworkX multigraph
A MultiGraph or MultiDiGraph.
Notes
-----
Use G=nx.Graph(nx.read_dot(path)) to return a Graph instead of a MultiGraph.
"""
try:
import pydot
except ImportError:
raise ImportError("read_dot() requires pydot",
"http://dkbza.org/pydot.html/")
fh=_get_fh(path,'r')
data=fh.read()
P=pydot.graph_from_dot_data(data)
return from_pydot(P)
示例15: write_yaml
def write_yaml(G, path, encoding='UTF-8', **kwds):
"""Write graph G in YAML format to path.
YAML is a data serialization format designed for human readability
and interaction with scripting languages [1]_.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
encoding: string, optional
Specify which encoding to use when writing file.
Examples
--------
>>> G=nx.path_graph(4)
>>> nx.write_yaml(G,'test.yaml')
References
----------
.. [1] http://www.yaml.org
"""
try:
import yaml
except ImportError:
raise ImportError("write_yaml() requires PyYAML: http://pyyaml.org/")
fh=_get_fh(path,mode='w')
yaml.dump(G,fh,**kwds)
fh.close()