我有一个csv文件,如下所示:
No String
1 A B A A B C D E E C F
1 B B B C M F G
1 A A M V
2 H C A A A B B N M F
2 N M H D D B A F F N M N
3 A C M G F F A A A
.. ....
我想将此文件转换为包含节点的图:A,B,C,D,E,F,G,H,M,N,V,它们之间的边是“否”列中的值考虑循环。
任何提示将不胜感激。
答案 0 :(得分:0)
我刚刚在浏览过程中看到了您的问题,但是可能有所延迟,但可能有帮助 首先创建一个有向图和一个列表 图= DiGraph() string_list = list#添加所有字符串
因此,首先您必须读取文件并将其放入您喜欢的任何数据结构中。在这种情况下,我将其放入字典
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = DictReader(csvfile)
data = [dict(x) for x in reader]
之后
for row in data: # for each item in the dictionary
string_list.extend(row['String'].split(' ')) # add a list of strings which are separated by "space" into a list. So that, you can access each character
for char in string_list: # for each element in the list (each character)
Graph.add_edge(row['No'], char) (create an edge between each "No" with each character )
print(Graph.edges())
这应该有效