Python 3.x 使用NetworkX提取edgelist时的编码问题
我正试图从斯坦福大学大型网络数据集集合中构建Wikispeedia图。我下载了一个档案,里面的文章是“URL编码的”。从tsv文件中提取很容易(参见代码)。但是,第一个节点应解释为“edán mac Gabráin”,而是显示文件中写入的“%C3%81ed%C3%A1n_mac_Gabr%C3%A1in” Python的read_edgelist文档说我可以添加编码类型,但找不到URL编码的名称,也称为百分比编码。我试了几次,但都没有成功 我还阅读了Python 3.x 使用NetworkX提取edgelist时的编码问题,python-3.x,networkx,urlencode,Python 3.x,Networkx,Urlencode,我正试图从斯坦福大学大型网络数据集集合中构建Wikispeedia图。我下载了一个档案,里面的文章是“URL编码的”。从tsv文件中提取很容易(参见代码)。但是,第一个节点应解释为“edán mac Gabráin”,而是显示文件中写入的“%C3%81ed%C3%A1n_mac_Gabr%C3%A1in” Python的read_edgelist文档说我可以添加编码类型,但找不到URL编码的名称,也称为百分比编码。我试了几次,但都没有成功 我还阅读了urllib.parse.unquote()函
urllib.parse.unquote()
函数,但我认为可能有一种比自己处理行更干净的方法,即在read\u edgelist()函数中输入一个编码名称
这是我的密码:
fh = open("links.tsv", 'rb')
G = nx.read_edgelist("links.tsv", create_using=nx.DiGraph(), encoding="utf-8")
fh.close()
print("Nodes:", len(G.nodes))
print("Edges:", len(G.edges))
print(list(G.nodes)[0])
这是我关于堆栈溢出的第一篇文章,我不知道是否/如何在不显示文件的情况下加入文件,因此这里是links.tsv的开头,如果有任何帮助:
# The list of all links between articles.
# Article names are URL-encoded; e.g., in Java they can be decoded using java.net.URLDecoder.decode(articleName, "UTF-8").
# FORMAT: linkSource linkTarget
#
# When publishing on this data set, please cite:
# (1) Robert West and Jure Leskovec:
# Human Wayfinding in Information Networks.
# 21st International World Wide Web Conference (WWW), 2012.
# (2) Robert West, Joelle Pineau, and Doina Precup:
# Wikispeedia: An Online Game for Inferring Semantic Distances between Concepts.
# 21st International Joint Conference on Artificial Intelligence (IJCAI), 2009.
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Bede
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Columba
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in D%C3%A1l_Riata
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Great_Britain
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Ireland
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Isle_of_Man
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Monarchy
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Orkney
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Picts
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Scotland
%C3%81ed%C3%A1n_mac_Gabr%C3%A1in Wales
%C3%85land 20th_century
%C3%85land Baltic_Sea
%C3%85land Crimean_War
%C3%85land Currency
也可以随意对表格提出批评/建议,因为我发现:)