I have this small-gephiready.tsv file that structured like this:
Source Target Type
1 2 Undirected
1 3 Undirected
1 4 Undirected
1 5 Undirected
1 6 Undirected
I want to open it via NetworkX in python environment, with code:
G = nx.read_edgelist("small-gephiready.tsv", nodetype=int)
However, it returns error "Failed to convert edge data (['Type']) to dictionary". I thought Type datas is string, so I added data=('Type',str)
after nodetype=int, and it returns str has no attribute decode.
Am I write the read_edge function wrong or there is something I have to checked in file? Thank you for your help.
This is my answer, that apparently if you give headers for a file, you need some parameters to read them. However, I need pandas read_csv function and toss it to network_x with from_pandas_edgelist function.
f = pd.read_csv("small-gephiready.tsv", sep='\t')
G = nx.from_pandas_edgelist(f, source='FromNodeId', target='ToNodeId')
I am not really sure, how it worked, but the below code seemed to have provided me with expected results:
Remove the Headers first, then use the below code
nx.read_edgelist("small-gephiready.tsv", nodetype=int, data=(("Type", str),))
It loaded the nodes and edges without any issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.