在Networkx中读取和写入错误pajek文件

时间:2013-08-31 17:57:58

标签: networkx

当我写入pajek文件然后使用Networkx库python读回同一文件时,我收到错误

   >>> G=nx.read_pajek("eatRS.net")

   >>> nx.write_pajek(G,"temp.net")

   >>> G1=nx.read_pajek("temp.net")

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<string>", line 2, in read_pajek
 File "/usr/local/lib/python2.7/dist-packages/networkx/utils/decorators.py", line 193, in _open_file
   result = func(*new_args, **kwargs)
 File "/usr/local/lib/python2.7/dist-packages/networkx/readwrite/pajek.py", line 132, in read_pajek
   return parse_pajek(lines)
 File "/usr/local/lib/python2.7/dist-packages/networkx/readwrite/pajek.py", line 168, in parse_pajek
splitline=shlex.split(str(next(lines)))
 File "/usr/lib/python2.7/shlex.py", line 279, in split
  return list(lex)
 File "/usr/lib/python2.7/shlex.py", line 269, in next
token = self.get_token()
 File "/usr/lib/python2.7/shlex.py", line 96, in get_token
  raw = self.read_token()
 File "/usr/lib/python2.7/shlex.py", line 172, in read_token
r aise ValueError, "No closing quotation"
 ValueError: No closing quotation

1 个答案:

答案 0 :(得分:0)

在networkx中创建图形,以pajek格式写入然后再返回可以正常工作。例如。与gnm_random_graph

import matplotlib.pyplot as np
n = 10
m = 20

G = nx.gnm_random_graph(n,m)
nx.write_pajek(G, "temp.net")
G1 = nx.read_pajek("temp.net")

仅当我编辑中间图时才有,例如<​​/ p>

"vertex one 0.3456 0.1234 box ic White fos 20

我遇到ValueError: No closing quotation错误吗?节点标签可以是数字或字符串,但如果它们包含空格,则必须引用该名称。从Pajek manual

  

标签 - 如果标签以字符A..Z或0..9开头,则第一个空白确定标签的结尾   (例如:vertex1),由更多单词组成的标签必须包含在特殊对中   字符(例如:“vertex 1”)


因此,我建议您检查输入文件“eatRS.net”。也许字符编码,引号不匹配(例如用"打开,用'结束)或节点标签中的换行符有问题?