One of your CSV files does not have a “source” header for sure. -- T.
On Sunday, 17 November 2013 at 01:28, Ahmed Abdeen Hamed wrote: > Thank you! > > It is not liking the headers of the csv files: > > > Ahmeds-MacBook-Pro:python-projects ahamedCOM$ python file-reader.py > Traceback (most recent call last): > File "file-reader.py", line 8, in <module> > g = Graph.DictList(vertices=None, edges=chain(reader1, reader2)) > File "/Library/Python/2.7/site-packages/igraph/__init__.py", line 2246, in > DictList > v1 = vertex_name_map[edge_data[efk_src]] > KeyError: 'source' > > > -Ahmed > > > > > > > On Sat, Nov 16, 2013 at 11:32 AM, Tamás Nepusz <[email protected] > (mailto:[email protected])> wrote: > > Hi, > > > > > How do you append to a graph from a file? > > > > > > Here is a scenario of what I am trying to do I have two different csv > > > files: > > 1. How about concatenating the two files first and then loading the > > combined file? > > > > 2. If you don’t want to concatenate the physical files, you can still > > concatenate the CSV reader objects that you use to read them: > > > > from itertools import chain > > > > reader1 = csv.DictReader(open(“file1.csv”)) > > reader2 = csv.DictReader(open(“file2.csv”)) > > > > g = Graph.DictList(vertices=None, edges=chain(reader1, reader2)) > > > > 3. If you already have a graph and you want to add edges from another file > > to it, you will need a lookup table that 1) maps vertex names from the > > first graph to the corresponding vertex IDs, 2) is able to create new IDs > > for vertex names from the second that it hasn’t seen yet. igraph’s > > UniqueIdGenerator object can help you with that: > > > > # Create the UniqueIdGenerator and pre-load it with the names from the > > first graph > > id_gen = UniqueIdGenerator() > > for name in first_graph.vs[“name”]: > > id_gen.add(name) > > > > # Read the second graph > > reader = csv.DictReader(open(“file2.csv”)) > > new_edges = [] > > for row in reader: > > new_edges.append(id_gen[row[“source”]], id_gen[row[“target”]]) > > > > # Add the new vertices and edges to the graph > > n = g.vcount() > > if n < len(id_gen): > > g.add_vertices(len(id_gen) - n) > > g.vs[“name”] = id_gen.values() > > g.add_edges(new_edges) > > > > I know that it’s a bit cumbersome; the Python interface desperately needs a > > Graph.union_by_name() function but I haven’t had time to add it yet. > > > > — > > T. > > > > > > _______________________________________________ > > igraph-help mailing list > > [email protected] (mailto:[email protected]) > > https://lists.nongnu.org/mailman/listinfo/igraph-help > > > > _______________________________________________ > igraph-help mailing list > [email protected] (mailto:[email protected]) > https://lists.nongnu.org/mailman/listinfo/igraph-help > >
_______________________________________________ igraph-help mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/igraph-help
